xref: /csrg-svn/old/vplot/vplot.c (revision 11516)
1*11516Sralph /*
2*11516Sralph  * Reads standard graphics input
3*11516Sralph  * Makes a plot on a 200 dot-per-inch 11" wide
4*11516Sralph  * Versatek plotter.
5*11516Sralph  *
6*11516Sralph  * Creates and leaves /usr/tmp/raster (1000 blocks)
7*11516Sralph  * which is the bitmap
8*11516Sralph  */
9*11516Sralph #include "stdio.h"
10*11516Sralph #include <signal.h>
11*11516Sralph 
12*11516Sralph #define	NB	88
13*11516Sralph #define BSIZ	512
14*11516Sralph #define	mapx(x)	((1536*((x)-botx)/del)+centx)
15*11516Sralph #define	mapy(y)	((1536*(del-(y)+boty)/del)-centy)
16*11516Sralph #define SOLID -1
17*11516Sralph #define DOTTED 014
18*11516Sralph #define SHORTDASHED 034
19*11516Sralph #define DOTDASHED 054
20*11516Sralph #define LONGDASHED 074
21*11516Sralph #define	SETSTATE	(('v'<<8)+1)
22*11516Sralph 
23*11516Sralph int	linmod	= SOLID;
24*11516Sralph int	again;
25*11516Sralph int	done1;
26*11516Sralph char	chrtab[][16];
27*11516Sralph int	plotcom[]	= { 0200, 0, 0};
28*11516Sralph int	eotcom[]		= { 0210, 0, 0};
29*11516Sralph char	blocks	[NB][BSIZ];
30*11516Sralph int	obuf[264];
31*11516Sralph int	lastx;
32*11516Sralph int	lasty;
33*11516Sralph double	topx	= 1536;
34*11516Sralph double	topy	= 1536;
35*11516Sralph double	botx	= 0;
36*11516Sralph double	boty	= 0;
37*11516Sralph int	centx;
38*11516Sralph int	centy;
39*11516Sralph double	delx	= 1536;
40*11516Sralph double	dely	= 1536;
41*11516Sralph double	del	= 1536;
42*11516Sralph 
43*11516Sralph struct	buf {
44*11516Sralph 	int	bno;
45*11516Sralph 	char	*block;
46*11516Sralph };
47*11516Sralph struct	buf	bufs[NB];
48*11516Sralph 
49*11516Sralph int	in, out;
50*11516Sralph char *picture = "/usr/tmp/raster";
51*11516Sralph 
52*11516Sralph main(argc, argv)
53*11516Sralph char **argv;
54*11516Sralph {
55*11516Sralph 	extern int onintr();
56*11516Sralph 	register i;
57*11516Sralph 
58*11516Sralph 	if (argc>1) {
59*11516Sralph 		in = open(argv[1], 0);
60*11516Sralph 		putpict();
61*11516Sralph 		exit(0);
62*11516Sralph 	}
63*11516Sralph 	signal(SIGTERM, onintr);
64*11516Sralph 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
65*11516Sralph 		signal(SIGINT, onintr);
66*11516Sralph another:
67*11516Sralph 	for (i=0; i<NB; i++) {
68*11516Sralph 		bufs[i].bno = -1;
69*11516Sralph 		bufs[i].block = blocks[i];
70*11516Sralph 	}
71*11516Sralph 	out = creat(picture, 0666);
72*11516Sralph 	in = open(picture, 0);
73*11516Sralph 	zseek(out, 32*32);
74*11516Sralph 	write(out, blocks[0], BSIZ);
75*11516Sralph /*delete following code when filsys deals properly with
76*11516Sralph holes in files*/
77*11516Sralph 	for(i=0;i<512;i++)
78*11516Sralph 		blocks[0][i] = 0;
79*11516Sralph 	zseek(out, 0);
80*11516Sralph 	for(i=0;i<32*32;i++)
81*11516Sralph 		write(out,blocks[0],512);
82*11516Sralph /**/
83*11516Sralph 	getpict();
84*11516Sralph 	for (i=0; i<NB; i++)
85*11516Sralph 		if (bufs[i].bno != -1) {
86*11516Sralph 			zseek(out, bufs[i].bno);
87*11516Sralph 			write(out, bufs[i].block, BSIZ);
88*11516Sralph 		}
89*11516Sralph 	putpict();
90*11516Sralph 	if (again) {
91*11516Sralph 		close(in);
92*11516Sralph 		close(out);
93*11516Sralph 		goto another;
94*11516Sralph 	}
95*11516Sralph 	exit(0);
96*11516Sralph }
97*11516Sralph 
98*11516Sralph getpict()
99*11516Sralph {
100*11516Sralph 	register x1, y1;
101*11516Sralph 
102*11516Sralph 	again = 0;
103*11516Sralph 	for (;;) switch (x1 = getc(stdin)) {
104*11516Sralph 
105*11516Sralph 	case 's':
106*11516Sralph 		botx = getw(stdin);
107*11516Sralph 		boty = getw(stdin);
108*11516Sralph 		topx = getw(stdin);
109*11516Sralph 		topy = getw(stdin);
110*11516Sralph 		delx = topx-botx;
111*11516Sralph 		dely = topy-boty;
112*11516Sralph 		if (dely/delx > 1536./2048.)
113*11516Sralph 			del = dely;
114*11516Sralph 		else
115*11516Sralph 			del = delx * (1566./2048.);
116*11516Sralph 		centx = 0;
117*11516Sralph 		centx = (2048 - mapx(topx)) / 2;
118*11516Sralph 		centy = 0;
119*11516Sralph 		centy = mapy(topy) / 2;
120*11516Sralph 		continue;
121*11516Sralph 
122*11516Sralph 	case 'l':
123*11516Sralph 		done1 |= 01;
124*11516Sralph 		x1 = mapx(getw(stdin));
125*11516Sralph 		y1 = mapy(getw(stdin));
126*11516Sralph 		lastx = mapx(getw(stdin));
127*11516Sralph 		lasty = mapy(getw(stdin));
128*11516Sralph 		line(x1, y1, lastx, lasty);
129*11516Sralph 		continue;
130*11516Sralph 
131*11516Sralph 	case 'm':
132*11516Sralph 		lastx = mapx(getw(stdin));
133*11516Sralph 		lasty = mapy(getw(stdin));
134*11516Sralph 		continue;
135*11516Sralph 
136*11516Sralph 	case 't':
137*11516Sralph 		done1 |= 01;
138*11516Sralph 		while ((x1 = getc(stdin)) != '\n')
139*11516Sralph 			plotch(x1);
140*11516Sralph 		continue;
141*11516Sralph 
142*11516Sralph 	case 'e':
143*11516Sralph 		if (done1) {
144*11516Sralph 			again++;
145*11516Sralph 			return;
146*11516Sralph 		}
147*11516Sralph 		continue;
148*11516Sralph 
149*11516Sralph 	case 'p':
150*11516Sralph 		done1 |= 01;
151*11516Sralph 		lastx = mapx(getw(stdin));
152*11516Sralph 		lasty = mapy(getw(stdin));
153*11516Sralph 		point(lastx, lasty);
154*11516Sralph 		point(lastx+1, lasty);
155*11516Sralph 		point(lastx, lasty+1);
156*11516Sralph 		point(lastx+1, lasty+1);
157*11516Sralph 		continue;
158*11516Sralph 
159*11516Sralph 	case 'n':
160*11516Sralph 		done1 |= 01;
161*11516Sralph 		x1 = mapx(getw(stdin));
162*11516Sralph 		y1 = mapy(getw(stdin));
163*11516Sralph 		line(lastx, lasty, x1, y1);
164*11516Sralph 		lastx = x1;
165*11516Sralph 		lasty = y1;
166*11516Sralph 		continue;
167*11516Sralph 
168*11516Sralph 	case 'f':
169*11516Sralph 		getw(stdin);
170*11516Sralph 		getc(stdin);
171*11516Sralph 		switch(getc(stdin)) {
172*11516Sralph 		case 't':
173*11516Sralph 			linmod = DOTTED;
174*11516Sralph 			break;
175*11516Sralph 		default:
176*11516Sralph 		case 'i':
177*11516Sralph 			linmod = SOLID;
178*11516Sralph 			break;
179*11516Sralph 		case 'g':
180*11516Sralph 			linmod = LONGDASHED;
181*11516Sralph 			break;
182*11516Sralph 		case 'r':
183*11516Sralph 			linmod = SHORTDASHED;
184*11516Sralph 			break;
185*11516Sralph 		case 'd':
186*11516Sralph 			linmod = DOTDASHED;
187*11516Sralph 			break;
188*11516Sralph 		}
189*11516Sralph 		while((x1=getc(stdin))!='\n')
190*11516Sralph 			if(x1==-1) return;
191*11516Sralph 		continue;
192*11516Sralph 
193*11516Sralph 	case 'd':
194*11516Sralph 		getw(stdin);
195*11516Sralph 		getw(stdin);
196*11516Sralph 		getw(stdin);
197*11516Sralph 		x1 = getw(stdin);
198*11516Sralph 		while (--x1 >= 0)
199*11516Sralph 			getw(stdin);
200*11516Sralph 		continue;
201*11516Sralph 
202*11516Sralph 	case -1:
203*11516Sralph 		return;
204*11516Sralph 
205*11516Sralph 	default:
206*11516Sralph 		printf("Botch\n");
207*11516Sralph 		return;
208*11516Sralph 	}
209*11516Sralph }
210*11516Sralph 
211*11516Sralph plotch(c)
212*11516Sralph register c;
213*11516Sralph {
214*11516Sralph 	register j;
215*11516Sralph 	register char *cp;
216*11516Sralph 	int i;
217*11516Sralph 
218*11516Sralph 	if (c<' ' || c >0177)
219*11516Sralph 		return;
220*11516Sralph 	cp = chrtab[c-' '];
221*11516Sralph 	for (i = -16; i<16; i += 2) {
222*11516Sralph 		c = *cp++;
223*11516Sralph 		for (j=7; j>=0; --j)
224*11516Sralph 			if ((c>>j)&1) {
225*11516Sralph 				point(lastx+6-j*2, lasty+i);
226*11516Sralph 				point(lastx+7-j*2, lasty+i);
227*11516Sralph 				point(lastx+6-j*2, lasty+i+1);
228*11516Sralph 				point(lastx+7-j*2, lasty+i+1);
229*11516Sralph 			}
230*11516Sralph 	}
231*11516Sralph 	lastx += 16;
232*11516Sralph }
233*11516Sralph 
234*11516Sralph int	f; /* versatec file number */
235*11516Sralph putpict()
236*11516Sralph {
237*11516Sralph 	register x, *ip, *op;
238*11516Sralph 	int y;
239*11516Sralph 
240*11516Sralph 	if (f==0){
241*11516Sralph 		f = open("/dev/vp0", 1);
242*11516Sralph 		if (f < 0) {
243*11516Sralph 			printf("Cannot open vp\n");
244*11516Sralph 			exit(1);
245*11516Sralph 		}
246*11516Sralph 		ioctl(f, SETSTATE, plotcom);
247*11516Sralph 	}
248*11516Sralph 	op = obuf;
249*11516Sralph 	lseek(in, 0L, 0);
250*11516Sralph 	for (y=0; y<2048; y++) {
251*11516Sralph 		if ((y&077) == 0)
252*11516Sralph 			read(in, blocks[0], 32*BSIZ);
253*11516Sralph 		for (x=0; x<32; x++)  {
254*11516Sralph 			ip = (int *)&blocks[x][(y&077)<<3];
255*11516Sralph 			*op++ = *ip++;
256*11516Sralph 			*op++ = *ip++;
257*11516Sralph 			*op++ = *ip++;
258*11516Sralph 			*op++ = *ip++;
259*11516Sralph 		}
260*11516Sralph 		*op++ = 0;
261*11516Sralph 		*op++ = 0;
262*11516Sralph 		*op++ = 0;
263*11516Sralph 		*op++ = 0;
264*11516Sralph 		if (y&1) {
265*11516Sralph 			write(f, (char *)obuf, sizeof(obuf));
266*11516Sralph 			op = obuf;
267*11516Sralph 		}
268*11516Sralph 	}
269*11516Sralph }
270*11516Sralph 
271*11516Sralph line(x0, y0, x1, y1)
272*11516Sralph register x0, y0;
273*11516Sralph {
274*11516Sralph 	int dx, dy;
275*11516Sralph 	int xinc, yinc;
276*11516Sralph 	register res1;
277*11516Sralph 	int res2;
278*11516Sralph 	int slope;
279*11516Sralph 
280*11516Sralph 	xinc = 1;
281*11516Sralph 	yinc = 1;
282*11516Sralph 	if ((dx = x1-x0) < 0) {
283*11516Sralph 		xinc = -1;
284*11516Sralph 		dx = -dx;
285*11516Sralph 	}
286*11516Sralph 	if ((dy = y1-y0) < 0) {
287*11516Sralph 		yinc = -1;
288*11516Sralph 		dy = -dy;
289*11516Sralph 	}
290*11516Sralph 	slope = xinc*yinc;
291*11516Sralph 	res1 = 0;
292*11516Sralph 	res2 = 0;
293*11516Sralph 	if (dx >= dy) while (x0 != x1) {
294*11516Sralph 	if((x0+slope*y0)&linmod)
295*11516Sralph 	if (((x0>>6) + ((y0&~077)>>1)) == bufs[0].bno)
296*11516Sralph 		bufs[0].block[((y0&077)<<3)+((x0>>3)&07)] |= 1 << (7-(x0&07));
297*11516Sralph 	else
298*11516Sralph 		point(x0, y0);
299*11516Sralph 		if (res1 > res2) {
300*11516Sralph 			res2 += dx - res1;
301*11516Sralph 			res1 = 0;
302*11516Sralph 			y0 += yinc;
303*11516Sralph 		}
304*11516Sralph 		res1 += dy;
305*11516Sralph 		x0 += xinc;
306*11516Sralph 	} else while (y0 != y1) {
307*11516Sralph 	if((x0+slope*y0)&linmod)
308*11516Sralph 	if (((x0>>6) + ((y0&~077)>>1)) == bufs[0].bno)
309*11516Sralph 		bufs[0].block[((y0&077)<<3)+((x0>>3)&07)] |= 1 << (7-(x0&07));
310*11516Sralph 	else
311*11516Sralph 		point(x0, y0);
312*11516Sralph 		if (res1 > res2) {
313*11516Sralph 			res2 += dy - res1;
314*11516Sralph 			res1 = 0;
315*11516Sralph 			x0 += xinc;
316*11516Sralph 		}
317*11516Sralph 		res1 += dx;
318*11516Sralph 		y0 += yinc;
319*11516Sralph 	}
320*11516Sralph 	if((x1+slope*y1)&linmod)
321*11516Sralph 	if (((x1>>6) + ((y1&~077)>>1)) == bufs[0].bno)
322*11516Sralph 		bufs[0].block[((y1&077)<<3)+((x1>>3)&07)] |= 1 << (7-(x1&07));
323*11516Sralph 	else
324*11516Sralph 		point(x1, y1);
325*11516Sralph }
326*11516Sralph 
327*11516Sralph point(x, y)
328*11516Sralph register x, y;
329*11516Sralph {
330*11516Sralph 	register bno;
331*11516Sralph 
332*11516Sralph 	bno = ((x&03700)>>6) + ((y&03700)>>1);
333*11516Sralph 	if (bno != bufs[0].bno) {
334*11516Sralph 		if (bno < 0 || bno >= 1024)
335*11516Sralph 			return;
336*11516Sralph 		getblk(bno);
337*11516Sralph 	}
338*11516Sralph 	bufs[0].block[((y&077)<<3)+((x>>3)&07)] |= 1 << (7-(x&07));
339*11516Sralph }
340*11516Sralph 
341*11516Sralph getblk(b)
342*11516Sralph register b;
343*11516Sralph {
344*11516Sralph 	register struct buf *bp1, *bp2;
345*11516Sralph 	register char *tp;
346*11516Sralph 
347*11516Sralph loop:
348*11516Sralph 	for (bp1 = bufs; bp1 < &bufs[NB]; bp1++) {
349*11516Sralph 		if (bp1->bno == b || bp1->bno == -1) {
350*11516Sralph 			tp = bp1->block;
351*11516Sralph 			for (bp2 = bp1; bp2>bufs; --bp2) {
352*11516Sralph 				bp2->bno = (bp2-1)->bno;
353*11516Sralph 				bp2->block = (bp2-1)->block;
354*11516Sralph 			}
355*11516Sralph 			bufs[0].bno = b;
356*11516Sralph 			bufs[0].block = tp;
357*11516Sralph 			return;
358*11516Sralph 		}
359*11516Sralph 	}
360*11516Sralph 	zseek(out, bufs[NB-1].bno);
361*11516Sralph 	write(out, bufs[NB-1].block, BSIZ);
362*11516Sralph 	zseek(in, b);
363*11516Sralph 	read(in, bufs[NB-1].block, BSIZ);
364*11516Sralph 	bufs[NB-1].bno = b;
365*11516Sralph 	goto loop;
366*11516Sralph }
367*11516Sralph 
368*11516Sralph onintr()
369*11516Sralph {
370*11516Sralph 	exit(1);
371*11516Sralph }
372*11516Sralph 
373*11516Sralph zseek(a, b)
374*11516Sralph {
375*11516Sralph 	return(lseek(a, (long)b*512, 0));
376*11516Sralph }
377