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

# Content
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 VIEW ourview = STDVIEW; /* view parameters */
25 int hresolu = 512; /* horizontal resolution */
26 int vresolu = 512; /* vertical resolution */
27 double pixaspect = 1.0; /* pixel aspect ratio */
28
29 int psample = 4; /* pixel sample size */
30 double maxdiff = .05; /* max. difference for interpolation */
31 double dstrpix = 0.67; /* square pixel distribution */
32
33 double dstrsrc = 0.0; /* square source distribution */
34 double shadthresh = .05; /* shadow threshold */
35 double shadcert = .5; /* shadow certainty */
36
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 int ambres = 32; /* ambient resolution */
43 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 double pixvalue();
60
61
62 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 render(zfile, oldfile) /* render the scene */
98 char *zfile, *oldfile;
99 {
100 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
101 float *zbar[MAXDIV+1]; /* z values */
102 int ypos; /* current scanline */
103 FILE *zfp;
104 COLOR *colptr;
105 float *zptr;
106 register int i;
107 /* check sampling */
108 if (psample < 1)
109 psample = 1;
110 else if (psample > MAXDIV)
111 psample = MAXDIV;
112 /* allocate scanlines */
113 for (i = 0; i <= psample; i++) {
114 scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
115 if (scanbar[i] == NULL)
116 goto memerr;
117 }
118 /* 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 zbar[i] = (float *)malloc(hresolu*sizeof(float));
126 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 /* write out boundaries */
135 fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
136 /* recover file and compute first */
137 i = salvage(oldfile);
138 if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF)
139 error(SYSTEM, "z file seek error in render");
140 ypos = vresolu-1 - i;
141 fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
142 /* compute scanlines */
143 for (ypos -= psample; ypos >= 0; ypos -= psample) {
144
145 pctdone = 100.0*(vresolu-ypos-psample)/vresolu;
146
147 colptr = scanbar[psample]; /* move base to top */
148 scanbar[psample] = scanbar[0];
149 scanbar[0] = colptr;
150 zptr = zbar[psample];
151 zbar[psample] = zbar[0];
152 zbar[0] = zptr;
153 /* fill base line */
154 fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
155 /* fill bar */
156 fillscanbar(scanbar, zbar, hresolu, ypos, psample);
157 /* write it out */
158 for (i = psample; i > 0; i--) {
159 if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
160 goto writerr;
161 if (fwritescan(scanbar[i],hresolu,stdout) < 0)
162 goto writerr;
163 }
164 if (zfp != NULL && fflush(zfp) == EOF)
165 goto writerr;
166 if (fflush(stdout) == EOF)
167 goto writerr;
168 }
169 /* compute residual */
170 colptr = scanbar[psample];
171 scanbar[psample] = scanbar[0];
172 scanbar[0] = colptr;
173 zptr = zbar[psample];
174 zbar[psample] = zbar[0];
175 zbar[0] = zptr;
176 if (ypos > -psample) {
177 fillscanline(scanbar[-ypos], zbar[-ypos], hresolu, 0, psample);
178 fillscanbar(scanbar-ypos, zbar-ypos, hresolu, 0, psample+ypos);
179 }
180 for (i = psample; i+ypos >= 0; i--) {
181 if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
182 goto writerr;
183 if (fwritescan(scanbar[i], hresolu, stdout) < 0)
184 goto writerr;
185 }
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 if (fflush(stdout) == EOF)
194 goto writerr;
195 for (i = 0; i <= psample; i++)
196 free((char *)scanbar[i]);
197 pctdone = 100.0;
198 return;
199 writerr:
200 error(SYSTEM, "write error in render");
201 memerr:
202 error(SYSTEM, "out of memory in render");
203 }
204
205
206 fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */
207 register COLOR *scanline;
208 register float *zline;
209 int xres, y, xstep;
210 {
211 int b = xstep;
212 double z;
213 register int i;
214
215 z = pixvalue(scanline[0], 0, y);
216 if (zline) zline[0] = z;
217
218 for (i = xstep; i < xres; i += xstep) {
219
220 z = pixvalue(scanline[i], i, y);
221 if (zline) zline[i] = z;
222
223 b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
224 i-xstep, y, xstep, 0, b/2);
225 }
226 if (i-xstep < xres-1) {
227 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 }
232 }
233
234
235 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
236 register COLOR *scanbar[];
237 register float *zbar[];
238 int xres, y, ysize;
239 {
240 COLOR vline[MAXDIV+1];
241 float zline[MAXDIV+1];
242 int b = ysize;
243 double z;
244 register int i, j;
245
246 for (i = 0; i < xres; i++) {
247
248 copycolor(vline[0], scanbar[0][i]);
249 copycolor(vline[ysize], scanbar[ysize][i]);
250 if (zbar[0]) {
251 zline[0] = zbar[0][i];
252 zline[ysize] = zbar[ysize][i];
253 }
254
255 b = fillsample(vline, zbar[0] ? zline : NULL,
256 i, y, 0, ysize, b/2);
257
258 for (j = 1; j < ysize; j++)
259 copycolor(scanbar[j][i], vline[j]);
260 if (zbar[0])
261 for (j = 1; j < ysize; j++)
262 zbar[j][i] = zline[j];
263 }
264 }
265
266
267 int
268 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
269 register COLOR *colline;
270 register float *zline;
271 int x, y;
272 int xlen, ylen;
273 int b;
274 {
275 extern double fabs();
276 double ratio;
277 double z;
278 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 if (b > 0
291 || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
292 || bigdiff(colline[0], colline[len], maxdiff)) {
293
294 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
295 if (zline) zline[len>>1] = z;
296 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 if (zline) zline[len>>1] = zline[len] * ratio;
304 ratio = 1.0 - ratio;
305 copycolor(ctmp, colline[0]);
306 scalecolor(ctmp, ratio);
307 addcolor(colline[len>>1], ctmp);
308 if (zline) zline[len>>1] += zline[0] * ratio;
309 ncut = 0;
310 }
311 /* recurse */
312 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
313
314 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
318 return(ncut);
319 }
320
321
322 double
323 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 viewray(thisray.rorg, thisray.rdir, &ourview,
330 (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
331
332 rayorigin(&thisray, NULL, PRIMARY, 1.0);
333
334 rayvalue(&thisray); /* trace ray */
335
336 copycolor(col, thisray.rcol); /* return color */
337
338 return(thisray.rot); /* return distance */
339 }
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
353 if ((fp = fopen(oldfile, "r")) == NULL) {
354 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 if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
362 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
363 error(WARNING, errmsg);
364 fclose(fp);
365 return(0);
366 }
367
368 if (x != hresolu || y != vresolu) {
369 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
370 oldfile);
371 error(USER, errmsg);
372 }
373
374 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
375 if (scanline == NULL)
376 error(SYSTEM, "out of memory in salvage");
377 for (y = 0; y < vresolu; y++) {
378 if (freadcolrs(scanline, hresolu, fp) < 0)
379 break;
380 if (fwritecolrs(scanline, hresolu, stdout) < 0)
381 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 }