ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.1
Committed: Thu Aug 6 16:45:01 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# User Rev Content
1 greg 2.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     char *err;
116     FILE *fp;
117     int hr, vr;
118     /* set up view */
119     if ((err = setview(&ourview)) != NULL) {
120     fprintf(stderr, "%s: %s\n", progname, err);
121     exit(1);
122     }
123     normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
124     /* open output file */
125     if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
126     if ((fp = fdopen(dup(outfd), "w")) == NULL)
127     goto filerr;
128     printargs(ac, av, fp); /* write header */
129     fputs(VIEWSTR, fp);
130     fprintview(&ourview, fp);
131     putc('\n', fp);
132     if (pixaspect < .99 || pixaspect > 1.01)
133     fputaspect(pixaspect, fp);
134     fputformat(COLRFMT, fp);
135     putc('\n', fp);
136     fprtresolu(hres*hmult, vres*vmult, fp);
137     } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
138     sleep(30); /* wait for header */
139     if ((fp = fdopen(dup(outfd), "r+")) == NULL)
140     goto filerr;
141     getheader(fp, NULL); /* skip header */
142     if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */
143     hr != hres*hmult || vr != vres*vmult) {
144     fprintf(stderr, "%s: picture resolution mismatch\n",
145     outfile);
146     exit(1);
147     }
148     } else {
149     fprintf(stderr, "%s: cannot open\n", outfile);
150     exit(1);
151     }
152     scanorig = ftell(fp); /* record position of first scanline */
153     if (fclose(fp) == -1) /* done with stream i/o */
154     goto filerr;
155     /* start rpict process */
156     if (open_process(rpd, rpargv) <= 0) {
157     fprintf(stderr, "%s: cannot start\n", rpargv[0]);
158     exit(1);
159     }
160     if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
161     (torp = fdopen(rpd[1], "w")) == NULL) {
162     fprintf(stderr, "%s: cannot open stream to %s\n",
163     progname, rpargv[0]);
164     exit(1);
165     }
166     if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
167     fprintf(stderr, "%s: out of memory\n", progname);
168     exit(1);
169     }
170     return;
171     filerr:
172     fprintf(stderr, "%s: file i/o error\n", outfile);
173     exit(1);
174     }
175    
176    
177     int
178     cleanup() /* close rpict process and clean up */
179     {
180     register int rpstat;
181    
182     free((char *)scanline);
183     fclose(torp);
184     fclose(fromrp);
185     rpstat = close_process(rpd);
186     if (rpstat == -1)
187     rpstat = 0;
188     return(rpstat);
189     }
190    
191    
192     rpiece() /* render picture piece by piece */
193     {
194     extern char *gets(), *strcat();
195     VIEW pview;
196     char buf[48];
197     int xorg, yorg;
198    
199     while (gets(buf) != NULL) {
200     if (sscanf(buf, "%d %d", &xorg, &yorg) != 2) {
201     fprintf(stderr, "%s: input format error\n", progname);
202     exit(1);
203     }
204     copystruct(&pview, &ourview); /* compute view for piece */
205     switch (ourview.type) {
206     case VT_PER:
207     pview.horiz = 2.*180./PI*atan(
208     tan(PI/180./2.*ourview.horiz)/hmult );
209     pview.vert = 2.*180./PI*atan(
210     tan(PI/180./2.*ourview.vert)/vmult );
211     break;
212     case VT_PAR:
213     case VT_ANG:
214     pview.horiz = ourview.horiz / hmult;
215     pview.vert = ourview.vert / vmult;
216     break;
217     case VT_HEM:
218     default:
219     fprintf(stderr, "%s: unknown view type '-vt%c'\n",
220     progname, ourview.type);
221     exit(1);
222     }
223     pview.hoff += xorg - 0.5*(hmult-1);
224     pview.voff += yorg - 0.5*(vmult-1);
225     fputs(VIEWSTR, torp);
226     fprintview(&pview, torp);
227     putc('\n', torp);
228     fflush(torp); /* assign piece to rpict */
229     putpiece(xorg, yorg); /* place piece in output */
230     if (verbose) { /* notify caller */
231     strcat(buf, " done\n");
232     writebuf(fileno(stdout), buf, strlen(buf));
233     }
234     }
235     }
236    
237    
238     putpiece(xpos, ypos) /* get next piece from rpict */
239     int xpos, ypos;
240     {
241     int hr, vr;
242     int y;
243    
244     getheader(fromrp, NULL); /* discard header info. */
245     if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
246     hr != hres || vr != vres) {
247     fprintf(stderr, "%s: resolution mismatch from rpict\n",
248     progname);
249     exit(1);
250     }
251     for (y = 0; y < vr; y++) { /* transfer scanlines */
252     if (freadcolrs(scanline, hr, fromrp) < 0) {
253     fprintf(stderr, "%s: read error from rpict\n",
254     progname);
255     exit(1);
256     }
257     if ((y == 0 || hmult != 1) && lseek(outfd,
258     scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
259     0) == -1) {
260     fprintf(stderr, "%s: seek error\n", outfile);
261     exit(1);
262     }
263     if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
264     hr*sizeof(COLR)) {
265     fprintf(stderr, "%s: write error\n", outfile);
266     exit(1);
267     }
268     }
269     }