ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.13
Committed: Tue Jan 9 09:07:29 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.12: +1 -0 lines
Log Message:
added option for pixel aspect ratio

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1986 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * rpict.c - routines and variables for picture generation.
9     *
10     * 8/14/85
11     */
12    
13     #include "ray.h"
14    
15     #ifdef BSD
16     #include <sys/time.h>
17     #include <sys/resource.h>
18     #endif
19    
20     #include "view.h"
21    
22     #include "random.h"
23    
24 greg 1.12 VIEW ourview = STDVIEW; /* view parameters */
25     int hresolu = 512; /* horizontal resolution */
26     int vresolu = 512; /* vertical resolution */
27 greg 1.13 double pixaspect = 1.0; /* pixel aspect ratio */
28 greg 1.1
29     int psample = 4; /* pixel sample size */
30     double maxdiff = .05; /* max. difference for interpolation */
31 greg 1.3 double dstrpix = 0.67; /* square pixel distribution */
32 greg 1.1
33     double dstrsrc = 0.0; /* square source distribution */
34 greg 1.4 double shadthresh = .05; /* shadow threshold */
35 greg 1.5 double shadcert = .5; /* shadow certainty */
36 greg 1.1
37     int maxdepth = 6; /* maximum recursion depth */
38     double minweight = 5e-3; /* minimum ray weight */
39    
40     COLOR ambval = BLKCOLOR; /* ambient value */
41     double ambacc = 0.2; /* ambient accuracy */
42 greg 1.6 int ambres = 32; /* ambient resolution */
43 greg 1.1 int ambdiv = 128; /* ambient divisions */
44     int ambssamp = 0; /* ambient super-samples */
45     int ambounce = 0; /* ambient bounces */
46     char *amblist[128]; /* ambient include/exclude list */
47     int ambincl = -1; /* include == 1, exclude == 0 */
48    
49     int ralrm = 0; /* seconds between reports */
50    
51     double pctdone = 0.0; /* percentage done */
52    
53     extern long nrays; /* number of rays traced */
54    
55     #define MAXDIV 32 /* maximum sample size */
56    
57     #define pixjitter() (.5+dstrpix*(.5-frandom()))
58    
59 greg 1.11 double pixvalue();
60 greg 1.1
61 greg 1.11
62 greg 1.1 quit(code) /* quit program */
63     int code;
64     {
65     if (code || ralrm > 0) /* report status */
66     report();
67    
68     exit(code);
69     }
70    
71    
72     report() /* report progress */
73     {
74     #ifdef BSD
75     struct rusage rubuf;
76     double t;
77    
78     getrusage(RUSAGE_SELF, &rubuf);
79     t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
80     t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
81     getrusage(RUSAGE_CHILDREN, &rubuf);
82     t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
83     t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
84    
85     sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
86     nrays, pctdone, t/3600.0);
87     #else
88     sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
89     #endif
90     eputs(errmsg);
91    
92     if (ralrm > 0)
93     alarm(ralrm);
94     }
95    
96    
97 greg 1.11 render(zfile, oldfile) /* render the scene */
98     char *zfile, *oldfile;
99 greg 1.1 {
100     COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
101 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
102 greg 1.1 int ypos; /* current scanline */
103 greg 1.11 FILE *zfp;
104 greg 1.1 COLOR *colptr;
105 greg 1.11 float *zptr;
106 greg 1.1 register int i;
107 greg 1.10 /* check sampling */
108     if (psample < 1)
109 greg 1.1 psample = 1;
110 greg 1.10 else if (psample > MAXDIV)
111     psample = MAXDIV;
112 greg 1.9 /* allocate scanlines */
113 greg 1.1 for (i = 0; i <= psample; i++) {
114 greg 1.12 scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
115 greg 1.1 if (scanbar[i] == NULL)
116 greg 1.11 goto memerr;
117 greg 1.1 }
118 greg 1.11 /* open z file */
119     if (zfile != NULL) {
120     if ((zfp = fopen(zfile, "a+")) == NULL) {
121     sprintf(errmsg, "cannot open z file \"%s\"", zfile);
122     error(SYSTEM, errmsg);
123     }
124     for (i = 0; i <= psample; i++) {
125 greg 1.12 zbar[i] = (float *)malloc(hresolu*sizeof(float));
126 greg 1.11 if (zbar[i] == NULL)
127     goto memerr;
128     }
129     } else {
130     zfp = NULL;
131     for (i = 0; i <= psample; i++)
132     zbar[i] = NULL;
133     }
134 greg 1.1 /* write out boundaries */
135 greg 1.12 fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
136 greg 1.10 /* recover file and compute first */
137 greg 1.11 i = salvage(oldfile);
138 greg 1.12 if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF)
139 greg 1.11 error(SYSTEM, "z file seek error in render");
140 greg 1.12 ypos = vresolu-1 - i;
141     fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
142 greg 1.10 /* compute scanlines */
143     for (ypos -= psample; ypos >= 0; ypos -= psample) {
144 greg 1.1
145 greg 1.12 pctdone = 100.0*(vresolu-ypos-psample)/vresolu;
146 greg 1.9
147     colptr = scanbar[psample]; /* move base to top */
148 greg 1.1 scanbar[psample] = scanbar[0];
149     scanbar[0] = colptr;
150 greg 1.11 zptr = zbar[psample];
151     zbar[psample] = zbar[0];
152     zbar[0] = zptr;
153 greg 1.10 /* fill base line */
154 greg 1.12 fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
155 greg 1.10 /* fill bar */
156 greg 1.12 fillscanbar(scanbar, zbar, hresolu, ypos, psample);
157 greg 1.10 /* write it out */
158 greg 1.11 for (i = psample; i > 0; i--) {
159 greg 1.12 if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
160 greg 1.1 goto writerr;
161 greg 1.12 if (fwritescan(scanbar[i],hresolu,stdout) < 0)
162 greg 1.11 goto writerr;
163     }
164     if (zfp != NULL && fflush(zfp) == EOF)
165     goto writerr;
166 greg 1.1 if (fflush(stdout) == EOF)
167     goto writerr;
168     }
169 greg 1.10 /* compute residual */
170     colptr = scanbar[psample];
171     scanbar[psample] = scanbar[0];
172     scanbar[0] = colptr;
173 greg 1.11 zptr = zbar[psample];
174     zbar[psample] = zbar[0];
175     zbar[0] = zptr;
176 greg 1.10 if (ypos > -psample) {
177 greg 1.12 fillscanline(scanbar[-ypos], zbar[-ypos], hresolu, 0, psample);
178     fillscanbar(scanbar-ypos, zbar-ypos, hresolu, 0, psample+ypos);
179 greg 1.10 }
180 greg 1.11 for (i = psample; i+ypos >= 0; i--) {
181 greg 1.12 if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
182 greg 1.11 goto writerr;
183 greg 1.12 if (fwritescan(scanbar[i], hresolu, stdout) < 0)
184 greg 1.10 goto writerr;
185 greg 1.11 }
186     /* clean up */
187     if (zfp != NULL) {
188     if (fclose(zfp) == EOF)
189     goto writerr;
190     for (i = 0; i <= psample; i++)
191     free((char *)zbar[i]);
192     }
193 greg 1.10 if (fflush(stdout) == EOF)
194     goto writerr;
195 greg 1.1 for (i = 0; i <= psample; i++)
196     free((char *)scanbar[i]);
197 greg 1.11 pctdone = 100.0;
198 greg 1.1 return;
199     writerr:
200     error(SYSTEM, "write error in render");
201 greg 1.11 memerr:
202     error(SYSTEM, "out of memory in render");
203 greg 1.1 }
204    
205    
206 greg 1.11 fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */
207 greg 1.1 register COLOR *scanline;
208 greg 1.11 register float *zline;
209 greg 1.9 int xres, y, xstep;
210 greg 1.1 {
211     int b = xstep;
212 greg 1.11 double z;
213 greg 1.1 register int i;
214    
215 greg 1.11 z = pixvalue(scanline[0], 0, y);
216     if (zline) zline[0] = z;
217 greg 1.1
218 greg 1.9 for (i = xstep; i < xres; i += xstep) {
219 greg 1.1
220 greg 1.11 z = pixvalue(scanline[i], i, y);
221     if (zline) zline[i] = z;
222 greg 1.1
223 greg 1.11 b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
224     i-xstep, y, xstep, 0, b/2);
225 greg 1.10 }
226     if (i-xstep < xres-1) {
227 greg 1.11 z = pixvalue(scanline[xres-1], xres-1, y);
228     if (zline) zline[xres-1] = z;
229     fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
230     i-xstep, y, xres-1-(i-xstep), 0, b/2);
231 greg 1.1 }
232     }
233    
234    
235 greg 1.11 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
236 greg 1.1 register COLOR *scanbar[];
237 greg 1.11 register float *zbar[];
238 greg 1.9 int xres, y, ysize;
239 greg 1.1 {
240     COLOR vline[MAXDIV+1];
241 greg 1.11 float zline[MAXDIV+1];
242 greg 1.1 int b = ysize;
243 greg 1.11 double z;
244 greg 1.1 register int i, j;
245    
246 greg 1.8 for (i = 0; i < xres; i++) {
247 greg 1.1
248     copycolor(vline[0], scanbar[0][i]);
249     copycolor(vline[ysize], scanbar[ysize][i]);
250 greg 1.11 if (zbar[0]) {
251     zline[0] = zbar[0][i];
252     zline[ysize] = zbar[ysize][i];
253     }
254 greg 1.1
255 greg 1.11 b = fillsample(vline, zbar[0] ? zline : NULL,
256     i, y, 0, ysize, b/2);
257 greg 1.1
258     for (j = 1; j < ysize; j++)
259     copycolor(scanbar[j][i], vline[j]);
260 greg 1.11 if (zbar[0])
261     for (j = 1; j < ysize; j++)
262     zbar[j][i] = zline[j];
263 greg 1.1 }
264     }
265    
266    
267     int
268 greg 1.11 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
269 greg 1.1 register COLOR *colline;
270 greg 1.11 register float *zline;
271 greg 1.1 int x, y;
272     int xlen, ylen;
273     int b;
274     {
275 greg 1.11 extern double fabs();
276 greg 1.1 double ratio;
277 greg 1.11 double z;
278 greg 1.1 COLOR ctmp;
279     int ncut;
280     register int len;
281    
282     if (xlen > 0) /* x or y length is zero */
283     len = xlen;
284     else
285     len = ylen;
286    
287     if (len <= 1) /* limit recursion */
288     return(0);
289    
290 greg 1.11 if (b > 0
291     || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
292     || bigdiff(colline[0], colline[len], maxdiff)) {
293 greg 1.1
294 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
295     if (zline) zline[len>>1] = z;
296 greg 1.1 ncut = 1;
297    
298     } else { /* interpolate */
299    
300     copycolor(colline[len>>1], colline[len]);
301     ratio = (double)(len>>1) / len;
302     scalecolor(colline[len>>1], ratio);
303 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
304     ratio = 1.0 - ratio;
305 greg 1.1 copycolor(ctmp, colline[0]);
306     scalecolor(ctmp, ratio);
307     addcolor(colline[len>>1], ctmp);
308 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
309 greg 1.1 ncut = 0;
310     }
311     /* recurse */
312 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
313 greg 1.1
314 greg 1.11 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
315     x+(xlen>>1), y+(ylen>>1),
316     xlen-(xlen>>1), ylen-(ylen>>1), b/2);
317 greg 1.1
318     return(ncut);
319     }
320    
321    
322 greg 1.11 double
323 greg 1.1 pixvalue(col, x, y) /* compute pixel value */
324     COLOR col; /* returned color */
325     int x, y; /* pixel position */
326     {
327     static RAY thisray; /* our ray for this pixel */
328    
329 greg 1.12 viewray(thisray.rorg, thisray.rdir, &ourview,
330     (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
331 greg 1.1
332     rayorigin(&thisray, NULL, PRIMARY, 1.0);
333    
334     rayvalue(&thisray); /* trace ray */
335    
336     copycolor(col, thisray.rcol); /* return color */
337 greg 1.11
338     return(thisray.rot); /* return distance */
339 greg 1.1 }
340    
341    
342     int
343     salvage(oldfile) /* salvage scanlines from killed program */
344     char *oldfile;
345     {
346     COLR *scanline;
347     FILE *fp;
348     int x, y;
349    
350     if (oldfile == NULL)
351     return(0);
352 greg 1.9
353     if ((fp = fopen(oldfile, "r")) == NULL) {
354 greg 1.1 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
355     error(WARNING, errmsg);
356     return(0);
357     }
358     /* discard header */
359     getheader(fp, NULL);
360     /* get picture size */
361 greg 1.7 if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
362 greg 1.2 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
363     error(WARNING, errmsg);
364 greg 1.1 fclose(fp);
365     return(0);
366     }
367    
368 greg 1.12 if (x != hresolu || y != vresolu) {
369 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
370     oldfile);
371     error(USER, errmsg);
372     }
373    
374 greg 1.12 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
375 greg 1.1 if (scanline == NULL)
376     error(SYSTEM, "out of memory in salvage");
377 greg 1.12 for (y = 0; y < vresolu; y++) {
378     if (freadcolrs(scanline, hresolu, fp) < 0)
379 greg 1.1 break;
380 greg 1.12 if (fwritecolrs(scanline, hresolu, stdout) < 0)
381 greg 1.1 goto writerr;
382     }
383     if (fflush(stdout) == EOF)
384     goto writerr;
385     free((char *)scanline);
386     fclose(fp);
387     unlink(oldfile);
388     return(y);
389     writerr:
390     error(SYSTEM, "write error in salvage");
391     }