ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.2
Committed: Thu Aug 6 17:16:29 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +2 -0 lines
Log Message:
added version string to output

File Contents

# Content
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\n", 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 default:
221 fprintf(stderr, "%s: unknown view type '-vt%c'\n",
222 progname, ourview.type);
223 exit(1);
224 }
225 pview.hoff += xorg - 0.5*(hmult-1);
226 pview.voff += yorg - 0.5*(vmult-1);
227 fputs(VIEWSTR, torp);
228 fprintview(&pview, torp);
229 putc('\n', torp);
230 fflush(torp); /* assign piece to rpict */
231 putpiece(xorg, yorg); /* place piece in output */
232 if (verbose) { /* notify caller */
233 strcat(buf, " done\n");
234 writebuf(fileno(stdout), buf, strlen(buf));
235 }
236 }
237 }
238
239
240 putpiece(xpos, ypos) /* get next piece from rpict */
241 int xpos, ypos;
242 {
243 int hr, vr;
244 int y;
245
246 getheader(fromrp, NULL); /* discard header info. */
247 if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
248 hr != hres || vr != vres) {
249 fprintf(stderr, "%s: resolution mismatch from rpict\n",
250 progname);
251 exit(1);
252 }
253 for (y = 0; y < vr; y++) { /* transfer scanlines */
254 if (freadcolrs(scanline, hr, fromrp) < 0) {
255 fprintf(stderr, "%s: read error from rpict\n",
256 progname);
257 exit(1);
258 }
259 if ((y == 0 || hmult != 1) && lseek(outfd,
260 scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
261 0) == -1) {
262 fprintf(stderr, "%s: seek error\n", outfile);
263 exit(1);
264 }
265 if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
266 hr*sizeof(COLR)) {
267 fprintf(stderr, "%s: write error\n", outfile);
268 exit(1);
269 }
270 }
271 }