xref: /plan9-contrib/sys/src/cmd/postscript/p9bitpost/p9bitpost.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <memdraw.h>
5 #include "pslib.h"
6 
7 #define HDLEN	60
8 
9 int dpi = -1;
10 int debug = 0;
11 int landscape = 0;
12 char *file = "<stdin>";
13 
14 void
15 error(void)
16 {
17 	fprint(2, "p9bitpost: can't read file %s: %r\n", file);
18 	exits("error");
19 }
20 
21 void
22 main(int argc, char *argv[]) {
23 	int i, fd = 0;
24 	double xmag = 1.0, ymag = 1.0;
25 	char *optstr, *Patch;
26 	Memimage *memimage;
27 
28 	Patch = nil;
29 	for (i=1; i<argc; i++) {
30 		if (*argv[i] != '-') break;
31 		switch(argv[i][1]) {
32 		case 'b':
33 			if (argv[i][2] == '\0')
34 				dpi = atoi(argv[++i]);
35 			else
36 				dpi = atoi(&(argv[i][2]));
37 			break;
38 		case 'd':
39 			debug = 1;
40 			break;
41 		case 'm':
42 			if (argv[i][2] == '\0')
43 				optstr = argv[++i];
44 			else
45 				optstr = &(argv[i][2]);
46 			if ((optstr=strtok(optstr, " ,")) != 0)
47 				xmag = ymag = atof(optstr);
48 			if ((optstr=strtok(0, " ,")) != 0)
49 				ymag = atof(optstr);
50 			break;
51 		case 'L':
52 			landscape = 1;
53 			break;
54 		case 'P':
55 			if (argv[i][2] == '\0')
56 				Patch = argv[++i];
57 			else
58 				Patch = &(argv[i][2]);
59 			break;
60 		default:
61 			fprint(2, "usage: %s [-b dpi] [-m magnification] [-L] [-P postscript_patch_string] inputfile\n", argv[0]);
62 			exits("usage");
63 		}
64 	}
65 
66 	if (i < argc) {
67 		file = argv[i];
68 		fd = open(file, OREAD);
69 		if (fd < 0)
70 			error();
71 	}
72 
73 	memimageinit();
74 	memimage = readmemimage(fd);
75 	if(memimage == nil)
76 		error();
77 
78 	psinit(0, 0);
79 	if(xmag != 1.0)
80 		psopt("xmagnification", &xmag);
81 	if(ymag != 1.0)
82 		psopt("ymagnification", &ymag);
83 	if(landscape)
84 		psopt("landscape", &landscape);
85 	if(Patch)
86 		psopt("Patch", &Patch);
87 	image2psfile(1, memimage, dpi);
88 	exits("");
89 }
90