1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Generate sections of a picture. |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
#include <fcntl.h> |
13 |
#include "color.h" |
14 |
#include "view.h" |
15 |
#include "resolu.h" |
16 |
|
17 |
/* rpict command */ |
18 |
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", }; |
19 |
int rpargc = 7; |
20 |
int rpd[3]; |
21 |
FILE *torp, *fromrp; |
22 |
COLR *scanline; |
23 |
/* our view parameters */ |
24 |
VIEW ourview = STDVIEW; |
25 |
double pixaspect = 0.0; |
26 |
int hres = 512, vres = 512, hmult = 2, vmult = 2; |
27 |
/* output file */ |
28 |
char *outfile = NULL; |
29 |
int outfd; |
30 |
long scanorig; |
31 |
|
32 |
char *progname; |
33 |
int verbose = 0; |
34 |
|
35 |
extern long lseek(), ftell(); |
36 |
|
37 |
|
38 |
main(argc, argv) |
39 |
int argc; |
40 |
char *argv[]; |
41 |
{ |
42 |
register int i, rval; |
43 |
|
44 |
progname = argv[0]; |
45 |
for (i = 1; i < argc; i++) { |
46 |
if (argv[i][0] == '-') |
47 |
switch (argv[i][1]) { |
48 |
case 'v': |
49 |
switch (argv[i][2]) { |
50 |
case '\0': /* verbose option */ |
51 |
verbose = !verbose; |
52 |
continue; |
53 |
case 'f': /* view file */ |
54 |
if (viewfile(argv[++i], &ourview, NULL) <= 0) { |
55 |
fprintf(stderr, |
56 |
"%s: not a view file\n", argv[i]); |
57 |
exit(1); |
58 |
} |
59 |
continue; |
60 |
default: /* view option? */ |
61 |
rval = getviewopt(&ourview, argc-i, argv+i); |
62 |
if (rval >= 0) { |
63 |
i += rval; |
64 |
continue; |
65 |
} |
66 |
break; |
67 |
} |
68 |
break; |
69 |
case 'p': /* pixel aspect ratio? */ |
70 |
if (argv[i][2] == 'a' && !argv[i][3]) |
71 |
pixaspect = atof(argv[i+1]); |
72 |
break; |
73 |
case 'x': /* piece x resolution */ |
74 |
if (!argv[i][2]) |
75 |
hres = atoi(argv[i+1]); |
76 |
break; |
77 |
case 'y': /* piece y resolution */ |
78 |
if (!argv[i][2]) |
79 |
vres = atoi(argv[i+1]); |
80 |
break; |
81 |
case 'X': /* horizontal multiplier */ |
82 |
if (argv[i][2]) |
83 |
break; |
84 |
hmult = atoi(argv[++i]); |
85 |
continue; |
86 |
case 'Y': /* vertical multiplier */ |
87 |
if (argv[i][2]) |
88 |
break; |
89 |
vmult = atoi(argv[++i]); |
90 |
continue; |
91 |
case 'o': /* output file */ |
92 |
if (argv[i][2]) |
93 |
break; |
94 |
outfile = argv[++i]; |
95 |
continue; |
96 |
} |
97 |
rpargv[rpargc++] = argv[i]; |
98 |
} |
99 |
rpargv[rpargc] = NULL; |
100 |
if (outfile == NULL) { |
101 |
fprintf(stderr, "%s: missing output file\n", argv[0]); |
102 |
exit(1); |
103 |
} |
104 |
init(argc, argv); |
105 |
rpiece(); |
106 |
rval = cleanup(); |
107 |
exit(rval); |
108 |
} |
109 |
|
110 |
|
111 |
init(ac, av) /* set up output file and start rpict */ |
112 |
int ac; |
113 |
char **av; |
114 |
{ |
115 |
extern char VersionID[]; |
116 |
char *err; |
117 |
FILE *fp; |
118 |
int hr, vr; |
119 |
/* set up view */ |
120 |
if ((err = setview(&ourview)) != NULL) { |
121 |
fprintf(stderr, "%s: %s\n", progname, err); |
122 |
exit(1); |
123 |
} |
124 |
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
125 |
/* open output file */ |
126 |
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
127 |
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
128 |
goto filerr; |
129 |
printargs(ac, av, fp); /* write header */ |
130 |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
131 |
fputs(VIEWSTR, fp); |
132 |
fprintview(&ourview, fp); |
133 |
putc('\n', fp); |
134 |
if (pixaspect < .99 || pixaspect > 1.01) |
135 |
fputaspect(pixaspect, fp); |
136 |
fputformat(COLRFMT, fp); |
137 |
putc('\n', fp); |
138 |
fprtresolu(hres*hmult, vres*vmult, fp); |
139 |
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
140 |
sleep(30); /* wait for header */ |
141 |
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
142 |
goto filerr; |
143 |
getheader(fp, NULL); /* skip header */ |
144 |
if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ |
145 |
hr != hres*hmult || vr != vres*vmult) { |
146 |
fprintf(stderr, "%s: picture resolution mismatch\n", |
147 |
outfile); |
148 |
exit(1); |
149 |
} |
150 |
} else { |
151 |
fprintf(stderr, "%s: cannot open\n", outfile); |
152 |
exit(1); |
153 |
} |
154 |
scanorig = ftell(fp); /* record position of first scanline */ |
155 |
if (fclose(fp) == -1) /* done with stream i/o */ |
156 |
goto filerr; |
157 |
/* start rpict process */ |
158 |
if (open_process(rpd, rpargv) <= 0) { |
159 |
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
160 |
exit(1); |
161 |
} |
162 |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
163 |
(torp = fdopen(rpd[1], "w")) == NULL) { |
164 |
fprintf(stderr, "%s: cannot open stream to %s\n", |
165 |
progname, rpargv[0]); |
166 |
exit(1); |
167 |
} |
168 |
if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) { |
169 |
fprintf(stderr, "%s: out of memory\n", progname); |
170 |
exit(1); |
171 |
} |
172 |
return; |
173 |
filerr: |
174 |
fprintf(stderr, "%s: file i/o error\n", outfile); |
175 |
exit(1); |
176 |
} |
177 |
|
178 |
|
179 |
int |
180 |
cleanup() /* close rpict process and clean up */ |
181 |
{ |
182 |
register int rpstat; |
183 |
|
184 |
free((char *)scanline); |
185 |
fclose(torp); |
186 |
fclose(fromrp); |
187 |
rpstat = close_process(rpd); |
188 |
if (rpstat == -1) |
189 |
rpstat = 0; |
190 |
return(rpstat); |
191 |
} |
192 |
|
193 |
|
194 |
rpiece() /* render picture piece by piece */ |
195 |
{ |
196 |
extern char *gets(), *strcat(); |
197 |
VIEW pview; |
198 |
char buf[48]; |
199 |
int xorg, yorg; |
200 |
|
201 |
while (gets(buf) != NULL) { |
202 |
if (sscanf(buf, "%d %d", &xorg, &yorg) != 2) { |
203 |
fprintf(stderr, "%s: input format error\n", progname); |
204 |
exit(1); |
205 |
} |
206 |
copystruct(&pview, &ourview); /* compute view for piece */ |
207 |
switch (ourview.type) { |
208 |
case VT_PER: |
209 |
pview.horiz = 2.*180./PI*atan( |
210 |
tan(PI/180./2.*ourview.horiz)/hmult ); |
211 |
pview.vert = 2.*180./PI*atan( |
212 |
tan(PI/180./2.*ourview.vert)/vmult ); |
213 |
break; |
214 |
case VT_PAR: |
215 |
case VT_ANG: |
216 |
pview.horiz = ourview.horiz / hmult; |
217 |
pview.vert = ourview.vert / vmult; |
218 |
break; |
219 |
case VT_HEM: |
220 |
pview.horiz = 2.*180./PI*asin( |
221 |
sin(PI/180./2.*ourview.horiz)/hmult ); |
222 |
pview.vert = 2.*180./PI*asin( |
223 |
sin(PI/180./2.*ourview.vert)/vmult ); |
224 |
break; |
225 |
default: |
226 |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
227 |
progname, ourview.type); |
228 |
exit(1); |
229 |
} |
230 |
pview.hoff += xorg - 0.5*(hmult-1); |
231 |
pview.voff += yorg - 0.5*(vmult-1); |
232 |
fputs(VIEWSTR, torp); |
233 |
fprintview(&pview, torp); |
234 |
putc('\n', torp); |
235 |
fflush(torp); /* assign piece to rpict */ |
236 |
putpiece(xorg, yorg); /* place piece in output */ |
237 |
if (verbose) { /* notify caller */ |
238 |
strcat(buf, " done\n"); |
239 |
writebuf(fileno(stdout), buf, strlen(buf)); |
240 |
} |
241 |
} |
242 |
} |
243 |
|
244 |
|
245 |
putpiece(xpos, ypos) /* get next piece from rpict */ |
246 |
int xpos, ypos; |
247 |
{ |
248 |
int hr, vr; |
249 |
int y; |
250 |
|
251 |
getheader(fromrp, NULL); /* discard header info. */ |
252 |
if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */ |
253 |
hr != hres || vr != vres) { |
254 |
fprintf(stderr, "%s: resolution mismatch from %s\n", |
255 |
progname, rpargv[0]); |
256 |
exit(1); |
257 |
} |
258 |
for (y = 0; y < vr; y++) { /* transfer scanlines */ |
259 |
if (freadcolrs(scanline, hr, fromrp) < 0) { |
260 |
fprintf(stderr, "%s: read error from %s\n", |
261 |
progname, rpargv[0]); |
262 |
exit(1); |
263 |
} |
264 |
if ((y == 0 || hmult != 1) && lseek(outfd, |
265 |
scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR), |
266 |
0) == -1) { |
267 |
fprintf(stderr, "%s: seek error\n", outfile); |
268 |
exit(1); |
269 |
} |
270 |
if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) != |
271 |
hr*sizeof(COLR)) { |
272 |
fprintf(stderr, "%s: write error\n", outfile); |
273 |
exit(1); |
274 |
} |
275 |
} |
276 |
} |