[comp.graphics] Raytrace to Impress/Postscript converter

koblas@mips.COM (David Koblas) (09/19/88)

Contained is a shar for converting MRGB pictures to either
impress or postscript depending on your needs (black and white).

[I'm looking for versatec plotter routines, if you have some I'd be interested]

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  Makefile README toprinter.c
# Wrapped by koblas@giant on Sun Sep 18 15:26:06 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(170 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X
all: prpress prpost
X
prpress: toprinter.c
X	$(CC) $(CFLAGS) -DIMPRESS -o $@ $?
X
prpost:  toprinter.c
X	$(CC) $(CFLAGS) -DPOSTSCRIPT -o $@ $?
X
clean:
X	rm -f prpress prpost
END_OF_FILE
if test 170 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(935 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
X	This shar contained programs to print MRGB imageformats,
onto either impress or postscript.  Man page is not included,
command line options are:
X
X	-x arg	(postscript only) set x printed size to arg inches.
X	-y arg	(postscript only) set y printed size to arg inches.
X	-S arg	(postscript only) set x and y printed size to arg inches.
X
X	-p arg	(impress only) set magnifcation factor (pixel size) to arg.
X
X	-i arg	input from file 'arg' otherwise stdin is used.
X	-r	disply only red component.
X	-g	disply only green component.
X	-b	disply only blue component.
X	-c	display a "rgb" picture (default).
X
The one item that I'm looking for is printing routines for a versatec plotter,
if you have some or know where I might be able to find some I'd be greatly
interested.
X
XFeedback is welcome - send bug reports, enhancements, to the address below:
X
X	David Koblas
X	koblas@mips.com		 koblas@uoregon.edu
X	{ ames, decwrl, pyramid, wyse }!mips!koblas
END_OF_FILE
if test 935 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'toprinter.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'toprinter.c'\"
else
echo shar: Extracting \"'toprinter.c'\" \(7319 characters\)
sed "s/^X//" >'toprinter.c' <<'END_OF_FILE'
X/* +------------------------------------------------------------------+ */
X/* | Copyright 1988, David Koblas.                                    | */
X/* |   You may copy this file in whole or in part as long as you      | */
X/* |   don't try to make money off it, or pretend that you wrote it.  | */
X/* +------------------------------------------------------------------+ */
X
X#include	<stdio.h>
X
X#define	R	0x01
X#define	G	0x02
X#define	B	0x04
X#define ALL	0x08
X
X#ifdef IMPRESS
X#define GREYMAX	(4)
X#define BITBLK	(32)	/* defined by impress (32 x 32 bits) */
X#define RPIXMAX	(2*456)	/* (RPIXMAX*GREYMAX)/BITBLK must be integrel */
X#define BLKMAX	((RPIXMAX * GREYMAX)/BITBLK)
X
unsigned char scanline[RPIXMAX][RPIXMAX];  /* buffer for linework */
unsigned char bitmap[BLKMAX][BLKMAX][BITBLK][GREYMAX];
X#endif
X
X#define RGB_TO_VAL(r,g,b)	((int)(((double)(r)*0.4)+ \
X				       ((double)(g)*0.4)+ \
X				       ((double)(b)*0.2)))
X#define VAL(x,y,i)		((i<3) ? data[(x+(xsize*y))*3+i] :\
X					 RGB_TO_VAL(data[(x+(xsize*y))*3+0],\
X					 	    data[(x+(xsize*y))*3+1],\
X					 	    data[(x+(xsize*y))*3+2]))
X
X#if !defined(POSTSCRIPT) && !defined(IMPRESS)
XEither POSTSCRIPT or IMPRESS needs to be defined!
X#endif
X
X#ifdef POSTSCRIPT
X#define OPTSTR "x:y:i:S:rgbc"
X#endif
X#ifdef IMPRESS
X#define OPTSTR "p:i:rgbc"
X#endif
X
char	*Progname;
X
usage()
X{
X	int	i;
X
X	fprintf(stderr,"usage: %s <args>\n",Progname);
X	for (i=0;i<strlen(OPTSTR);i++) {
X		switch (OPTSTR[i]) {
X		case 'r':
X			fprintf(stderr,"\t-r\tdisplay red space\n");
X			break;
X		case 'g':
X			fprintf(stderr,"\t-r\tdisplay green space\n");
X			break;
X		case 'b':
X			fprintf(stderr,"\t-r\tdisplay blue space\n");
X			break;
X		case 'c':
X			fprintf(stderr,"\t-c\tdisplay rgb space\n");
X			break;
X		case 'p':
X			fprintf(stderr,"\t-p arg\tscale pixel size to arg\n");
X			break;
X		case 'x':
X			fprintf(stderr,"\t-x arg\tset x printed size to be arg inches\n");
X			break;
X		case 'y':
X			fprintf(stderr,"\t-y arg\tset y printed size to be arg inches\n");
X			break;
X		case 'S':
X			fprintf(stderr,"\t-S arg\tset x and y printed size to be arg inches\n");
X			break;
X		case 'i': 
X			fprintf(stderr,"\t-i arg\tuse arg as the input file otherwise stdin is used\n");
X			break;
X		case ':': 
X			break;
X		default : 
X			fprintf(stderr,"\tUsage(%c)?\n",OPTSTR[i]); 
X			break;
X		}
X	}
X	exit(1);
X}
X 
main(argc,argv)
int	argc;
char	**argv;
X{
X	int		xsize,ysize;
X	double		xpsize=7.0,ypsize=7.0;
X	double		xpos,ypos;
X	double		atof();
X	int		c,x,y,i=0;
X	int		pixel=1;
X	char		*data;
X	int		flag=0;
X	extern char	*optarg;
X	extern int	optind;
X
X	Progname=argv[0]+strlen(argv[0]);
X	while ((*Progname != '/') && (Progname != argv[0])) Progname--;
X	if (*Progname=='/') Progname++;
X
X	while ((c=getopt(argc,argv,OPTSTR))!=EOF) {
X		switch(c) {
X			case 'r': flag|=R; break;
X			case 'g': flag|=G; break;
X			case 'b': flag|=B; break;
X			case 'c': flag|=ALL; break;
X			case 'p': pixel=atoi(optarg); break;
X			case 'x': xpsize=atof(optarg); break;
X			case 'y': ypsize=atof(optarg); break;
X			case 'S': xpsize=ypsize=atof(optarg); break;
X			case 'i': freopen(optarg,"r",stdin); break;
X			default : usage();
X		}
X	}
X	if (optind!=argc)
X		usage();
X	if (flag==0)
X		flag=ALL;
X
X	scanf("%d %d\n",&xsize,&ysize);
X	if ((data=(char *)malloc(xsize*ysize*sizeof(char)*3))==NULL)
X		exit(1);
X	fread(data,sizeof(char),xsize*ysize*3,stdin);
X
X#ifdef POSTSCRIPT
X	xpos    = (( 8.5-xpsize)/2.0)*72;
X	ypos    = ((11.0-ypsize)/2.0)*72;
X#endif
X#ifdef IMPRESS
X	xpos    = (( 8.5-((double)(pixel*xsize*GREYMAX)/300.0))/2.0)*300.0;
X	ypos    = ((11.0-((double)(pixel*ysize*GREYMAX)/300.0))/2.0)*300.0;
X#endif
X
X	i=0;
X	do {
X		if ((flag&(1<<i))==0) continue;
X
X#ifdef POSTSCRIPT
X		printf("%%!\nsave\n%d %d translate\n%d %d scale\n",
X			(int)xpos,(int)ypos,(int)xpsize*72,(int)ypsize*72);
X		printf("/DataString %d string def\n",ysize);
X		printf("%d %d 8 [ %d 0 0 %d 0 0 ] \n",
X			ysize,xsize,ysize,xsize);
X		printf("{\ncurrentfile DataString readhexstring pop\n} bind image\n");
X
X		for (y=0;y<ysize;y++) {
X			for (x=0;x<xsize;x++) {
X				printf("%02x",VAL(x,y,i));
X				if (i<3)
X					printf("%02x",data[(x+(xsize*y))*3+i]);
X				else
X					printf("%02x",RGB_TO_VAL(
X			}
X		}
X		printf("showpage\n");
X#endif
X#ifdef IMPRESS
X		printf("@document(language impress)");
X		putchar(213);   /* PAGE          */
X		putchar(135);	/* SET-ABS-H     */
X		putchar((((int)(xpos))>>8)&0x7f); 
X		putchar( ((int)(xpos))    &0xff); 
X		putchar(137);	/* SET-ABS-V     */
X		putchar((((int)(ypos))>>8)&0x7f); 
X		putchar( ((int)(ypos))    &0xff); 
X		putchar(236);   /* MAGNIFICATION */
X		putchar((pixel-1)&0xff);
X
X		putchar(235); /* BITMAP          */
X		putchar(15);  /* OPERATION (OR)  */
X		putchar(1+(int)((xsize*(GREYMAX))/BITBLK));	/* width   */
X		putchar(1+(int)((ysize*(GREYMAX))/BITBLK));	/* heighth */
X
X		for (x=0;x<RPIXMAX;x++)
X			for (y=0;y<RPIXMAX;y++)
X				scanline[y][x]=255;
X		
X		for (y=0;y<ysize;y++) {
X			for (x=0;x<xsize;x++) {
X				int	v  = (((double)VAL(x,y,i))*255.0/200.0);
X				if (v>255) v=255;
X				scanline[y][x] = v;
X			}
X		}
X
X		dobits(xsize,ysize);
X		putchar(219); /* SHOWPAGE */
X#endif
X	} while (i++<4);
X}
X
X#ifdef IMPRESS
X/*
X** Thanks to Jeff Eaton for these routies.
X*/
unsigned char greys[17][GREYMAX] = {
X    { 0xf, 0xf, 0xf, 0xf },       /* 1111 1111 1111 1111 */
X    { 0xf, 0xf, 0xf, 0x7 },       /* 1111 1111 1111 0111 */
X    { 0xf, 0xd, 0xf, 0x7 },       /* 1111 1101 1111 0111 */
X    { 0xf, 0xd, 0xf, 0x5 },       /* 1111 1101 1111 0101 */
X    { 0xf, 0x5, 0xf, 0x5 },       /* 1111 0101 1111 0101 */
X    { 0xf, 0x5, 0xb, 0x5 },       /* 1111 0101 1011 0101 */
X    { 0xe, 0x5, 0xb, 0x5 },       /* 1110 0101 1011 0101 */
X    { 0xe, 0x5, 0xa, 0x5 },       /* 1110 0101 1010 0101 */
X    { 0xa, 0x5, 0xa, 0x5 },       /* 1010 0101 1010 0101 */
X    { 0xa, 0x5, 0xa, 0x1 },       /* 1010 0101 1010 0001 */
X    { 0xa, 0x4, 0xa, 0x1 },       /* 1010 0100 1010 0001 */
X    { 0xa, 0x4, 0xa, 0x0 },       /* 1010 0100 1010 0000 */
X    { 0xa, 0x0, 0xa, 0x0 },       /* 1010 0000 1010 0000 */
X    { 0xa, 0x0, 0x2, 0x0 },       /* 1010 0000 0010 0000 */
X    { 0x8, 0x0, 0x2, 0x0 },       /* 1000 0000 0010 0000 */
X    { 0x8, 0x0, 0x0, 0x0 },       /* 1000 0000 0000 0000 */
X    { 0x0, 0x0, 0x0, 0x0 },       /* 0000 0000 0000 0000 */
X};
X
int dither[4][4] = {
X	{ 00,  8, 02, 10 },
X	{ 12, 04, 14, 06 },
X	{ 03, 11, 01,  9 },
X	{ 15, 07, 13, 05 },
X};
X
dobits(width,height)
int	width,height;
X{
X	register int row, col, lev, val1, val2, val;
X
X	for (row = 0; row < height; row++) {
X		for (col = 0; col < width; col += 2) {
X			val1 = scanline[row][col];
X			if (val1%16 > dither[row%4][col%4])
X				val1 = val1/16 + 1;
X			else
X				val1 = val1/16;
X			val2 = scanline[row][col+1];
X			if (val2%16 > dither[row%4][(col+1)%4])
X				val2 = val2/16 + 1;
X			else
X				val2 = val2/16;
X			for (lev = 0; lev < 4; lev++) {
X				val = greys[val2][lev]|(greys[val1][lev] << 4);
X				bitmap[row/8][col/8][(row%8)*4+lev][(col%8)/2] = val;
X			}
X		}
X	}
X	for (; row < RPIXMAX; row++) {
X		for (; col < RPIXMAX; col++) {
X			for (lev = 0; lev < 4; lev++) {
X				bitmap[row/8][col/8][(row%8)*4+lev][(col%8)/2] = 0;
X			}
X		}
X	}
X	for (val1 = 0; val1 <= (int)((height*GREYMAX)/BITBLK); val1++) {
X		for (lev = 0; lev <= (int)((width*GREYMAX)/BITBLK); lev++) {
X			for (row = 0; row < BITBLK; row++) {
X				for (col = 0; col < GREYMAX; col++) {
X					putchar(bitmap[val1][lev][row][col]);
X				}
X			}
X		}
X	}
X}
X#endif /* IMPRESS */
END_OF_FILE
if test 7319 -ne `wc -c <'toprinter.c'`; then
    echo shar: \"'toprinter.c'\" unpacked with wrong size!
fi
# end of 'toprinter.c'
fi
echo shar: End of shell archive.
exit 0
-- 
name : David Koblas         place: MIPS Computers Systems                
phone: 408-991-0287         uucp : {ames,decwrl,pyramid,wyse}!mips!koblas 
quote:  I've began to wonder if X11 is really a competition to see who can
        create the largest copyright that says, "It's free."

koblas@mips.COM (David Koblas) (09/20/88)

Patch #1:
[ For those of you who don't have patch or don't what to use it..
  all it says is to delete the four lines with a '-' infront of them.]

*** xxx	Mon Sep 19 16:14:20 1988
--- toprinter.c	Mon Sep 19 16:14:22 1988
***************
*** 153,162 ****
  		for (y=0;y<ysize;y++) {
  			for (x=0;x<xsize;x++) {
  				printf("%02x",VAL(x,y,i));
- 				if (i<3)
- 					printf("%02x",data[(x+(xsize*y))*3+i]);
- 				else
- 					printf("%02x",RGB_TO_VAL(
  			}
  		}
  		printf("showpage\n");
--- 153,158 ----


-- 
name : David Koblas         place: MIPS Computers Systems                
phone: 408-991-0287         uucp : {ames,decwrl,pyramid,wyse}!mips!koblas 
quote:  I've began to wonder if X11 is really a competition to see who can
        create the largest copyright that says, "It's free."