ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.52
Committed: Sat Feb 22 02:07:29 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.51: +90 -21 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.52 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4     /*
5     * rpict.c - routines and variables for picture generation.
6     */
7    
8 greg 2.52 /* ====================================================================
9     * The Radiance Software License, Version 1.0
10     *
11     * Copyright (c) 1990 - 2002 The Regents of the University of California,
12     * through Lawrence Berkeley National Laboratory. All rights reserved.
13     *
14     * Redistribution and use in source and binary forms, with or without
15     * modification, are permitted provided that the following conditions
16     * are met:
17     *
18     * 1. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 2. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in
23     * the documentation and/or other materials provided with the
24     * distribution.
25     *
26     * 3. The end-user documentation included with the redistribution,
27     * if any, must include the following acknowledgment:
28     * "This product includes Radiance software
29     * (http://radsite.lbl.gov/)
30     * developed by the Lawrence Berkeley National Laboratory
31     * (http://www.lbl.gov/)."
32     * Alternately, this acknowledgment may appear in the software itself,
33     * if and wherever such third-party acknowledgments normally appear.
34     *
35     * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36     * and "The Regents of the University of California" must
37     * not be used to endorse or promote products derived from this
38     * software without prior written permission. For written
39     * permission, please contact [email protected].
40     *
41     * 5. Products derived from this software may not be called "Radiance",
42     * nor may "Radiance" appear in their name, without prior written
43     * permission of Lawrence Berkeley National Laboratory.
44     *
45     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48     * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56     * SUCH DAMAGE.
57     * ====================================================================
58     *
59     * This software consists of voluntary contributions made by many
60     * individuals on behalf of Lawrence Berkeley National Laboratory. For more
61     * information on Lawrence Berkeley National Laboratory, please see
62     * <http://www.lbl.gov/>.
63     */
64    
65 greg 1.1 #include "ray.h"
66    
67 greg 2.32 #include <sys/types.h>
68    
69 greg 2.30 #ifndef NIX
70 greg 1.1 #ifdef BSD
71     #include <sys/time.h>
72     #include <sys/resource.h>
73 greg 2.30 #else
74     #include <sys/times.h>
75 greg 2.33 #include <unistd.h>
76 greg 1.34 #endif
77 greg 2.30 #endif
78 greg 1.34
79 greg 2.32 extern time_t time();
80    
81 greg 1.14 #include <signal.h>
82 greg 1.1
83     #include "view.h"
84    
85     #include "random.h"
86    
87 greg 2.12 #include "paths.h"
88    
89 greg 2.21 #define RFTEMPLATE "rfXXXXXX"
90    
91 greg 2.22 #ifndef SIGCONT
92     #define SIGCONT SIGIO
93     #endif
94    
95 greg 2.52 CUBE thescene; /* our scene */
96     OBJECT nsceneobjs; /* number of objects in our scene */
97    
98 greg 1.25 int dimlist[MAXDIM]; /* sampling dimensions */
99     int ndims = 0; /* number of sampling dimensions */
100     int samplendx; /* sample index number */
101    
102 greg 2.52 extern void ambnotify();
103     void (*addobjnotify[])() = {ambnotify, NULL};
104    
105 greg 1.12 VIEW ourview = STDVIEW; /* view parameters */
106     int hresolu = 512; /* horizontal resolution */
107     int vresolu = 512; /* vertical resolution */
108 greg 2.14 double pixaspect = 1.0; /* pixel aspect ratio */
109 greg 1.1
110     int psample = 4; /* pixel sample size */
111 greg 2.14 double maxdiff = .05; /* max. difference for interpolation */
112     double dstrpix = 0.67; /* square pixel distribution */
113 greg 1.1
114 gwlarson 2.49 double mblur = 0.; /* motion blur parameter */
115    
116 greg 2.52 void (*trace)() = NULL; /* trace call */
117    
118     int do_irrad = 0; /* compute irradiance? */
119    
120 greg 2.14 double dstrsrc = 0.0; /* square source distribution */
121     double shadthresh = .05; /* shadow threshold */
122     double shadcert = .5; /* shadow certainty */
123 greg 1.35 int directrelay = 1; /* number of source relays */
124 greg 1.29 int vspretest = 512; /* virtual source pretest density */
125 greg 2.20 int directvis = 1; /* sources visible? */
126 greg 2.14 double srcsizerat = .25; /* maximum ratio source size/dist. */
127 greg 2.41
128     COLOR cextinction = BLKCOLOR; /* global extinction coefficient */
129 greg 2.46 COLOR salbedo = BLKCOLOR; /* global scattering albedo */
130 greg 2.41 double seccg = 0.; /* global scattering eccentricity */
131     double ssampdist = 0.; /* scatter sampling distance */
132 greg 1.1
133 greg 2.14 double specthresh = .15; /* specular sampling threshold */
134     double specjitter = 1.; /* specular sampling jitter */
135 greg 2.5
136 greg 2.40 int backvis = 1; /* back face visibility */
137    
138 greg 1.1 int maxdepth = 6; /* maximum recursion depth */
139 greg 2.14 double minweight = 5e-3; /* minimum ray weight */
140 greg 1.1
141 greg 2.52 char *ambfile = NULL; /* ambient file name */
142 greg 1.1 COLOR ambval = BLKCOLOR; /* ambient value */
143 greg 2.44 int ambvwt = 0; /* initial weight for ambient value */
144 greg 2.14 double ambacc = 0.2; /* ambient accuracy */
145 greg 1.6 int ambres = 32; /* ambient resolution */
146 greg 1.1 int ambdiv = 128; /* ambient divisions */
147     int ambssamp = 0; /* ambient super-samples */
148     int ambounce = 0; /* ambient bounces */
149     char *amblist[128]; /* ambient include/exclude list */
150     int ambincl = -1; /* include == 1, exclude == 0 */
151    
152     int ralrm = 0; /* seconds between reports */
153    
154 greg 2.14 double pctdone = 0.0; /* percentage done */
155 greg 2.32 time_t tlastrept = 0L; /* time at last report */
156 greg 2.52 time_t tstart; /* starting time */
157 greg 1.1
158 greg 2.14 #define MAXDIV 16 /* maximum sample size */
159 greg 1.1
160 greg 2.14 #define pixjitter() (.5+dstrpix*(.5-frandom()))
161 greg 1.1
162 greg 2.47 int hres, vres; /* resolution for this frame */
163 greg 2.8
164 gwlarson 2.49 static VIEW lastview; /* the previous view input */
165    
166 greg 2.8 extern char *mktemp();
167    
168 greg 2.52 void report();
169    
170 greg 2.14 double pixvalue();
171 greg 1.1
172 greg 2.27 #ifdef NIX
173     #define file_exists(f) (access(f,F_OK)==0)
174     #else
175     #include <sys/types.h>
176     #include <sys/stat.h>
177     int
178     file_exists(fname) /* ordinary file exists? */
179     char *fname;
180     {
181     struct stat sbuf;
182     if (stat(fname, &sbuf) < 0) return(0);
183     return((sbuf.st_mode & S_IFREG) != 0);
184     }
185     #endif
186 greg 1.11
187 greg 2.27
188 greg 2.52 void
189 greg 1.1 quit(code) /* quit program */
190     int code;
191     {
192 greg 2.8 if (code) /* report status */
193 greg 1.1 report();
194 greg 2.52 #ifndef NIX
195 greg 2.21 headclean(); /* delete header file */
196     pfclean(); /* clean up persist files */
197 greg 2.52 #endif
198 greg 1.1 exit(code);
199     }
200    
201    
202 greg 2.30 #ifndef NIX
203 greg 2.52 void
204 greg 1.1 report() /* report progress */
205     {
206 greg 2.43 extern char *myhostname();
207 greg 2.30 double u, s;
208     #ifdef BSD
209 greg 1.1 struct rusage rubuf;
210 greg 2.30 #else
211     struct tms tbuf;
212 greg 2.33 double period;
213 greg 2.30 #endif
214 greg 1.1
215 greg 2.32 tlastrept = time((time_t *)NULL);
216 greg 2.30 #ifdef BSD
217 greg 1.1 getrusage(RUSAGE_SELF, &rubuf);
218 greg 2.30 u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
219     s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
220 greg 1.1 getrusage(RUSAGE_CHILDREN, &rubuf);
221 greg 2.30 u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
222     s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
223     #else
224     times(&tbuf);
225 greg 2.36 #ifdef _SC_CLK_TCK
226 greg 2.33 period = 1.0 / sysconf(_SC_CLK_TCK);
227 greg 2.36 #else
228     period = 1.0 / 60.0;
229     #endif
230 greg 2.33 u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
231     s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
232 greg 2.30 #endif
233 greg 1.1
234 greg 2.30 sprintf(errmsg,
235     "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
236     nrays, pctdone, u/3600., s/3600.,
237 greg 2.43 (tlastrept-tstart)/3600., myhostname());
238 greg 1.24 eputs(errmsg);
239 greg 2.35 #ifndef BSD
240     signal(SIGCONT, report);
241     #endif
242 greg 1.24 }
243 greg 1.1 #else
244 greg 2.52 void
245 greg 1.24 report() /* report progress */
246     {
247 greg 2.32 tlastrept = time((time_t *)NULL);
248 greg 2.30 sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
249 greg 1.24 nrays, pctdone, (tlastrept-tstart)/3600.0);
250 greg 1.1 eputs(errmsg);
251     }
252 greg 1.24 #endif
253 greg 1.1
254    
255 greg 2.52 void
256 greg 2.8 rpict(seq, pout, zout, prvr) /* generate image(s) */
257     int seq;
258     char *pout, *zout, *prvr;
259     /*
260     * If seq is greater than zero, then we will render a sequence of
261     * images based on view parameter strings read from the standard input.
262     * If pout is NULL, then all images will be sent to the standard ouput.
263     * If seq is greater than zero and prvr is an integer, then it is the
264     * frame number at which rendering should begin. Preceeding view parameter
265     * strings will be skipped in the input.
266     * If pout and prvr are the same, prvr is renamed to avoid overwriting.
267     * Note that pout and zout should contain %d format specifications for
268     * sequenced file naming.
269     */
270     {
271 greg 2.21 extern char *rindex(), *strncpy(), *strcat(), *strcpy();
272 greg 2.9 char fbuf[128], fbuf2[128];
273 greg 2.26 int npicts;
274 greg 2.9 register char *cp;
275 greg 2.14 RESOLU rs;
276     double pa;
277 greg 2.8 /* check sampling */
278     if (psample < 1)
279     psample = 1;
280     else if (psample > MAXDIV) {
281     sprintf(errmsg, "pixel sampling reduced from %d to %d",
282     psample, MAXDIV);
283     error(WARNING, errmsg);
284     psample = MAXDIV;
285     }
286     /* get starting frame */
287     if (seq <= 0)
288     seq = 0;
289     else if (prvr != NULL && isint(prvr)) {
290 greg 2.9 register int rn; /* skip to specified view */
291 greg 2.8 if ((rn = atoi(prvr)) < seq)
292     error(USER, "recover frame less than start frame");
293     if (pout == NULL)
294     error(USER, "missing output file specification");
295     for ( ; seq < rn; seq++)
296     if (nextview(stdin) == EOF)
297     error(USER, "unexpected EOF on view input");
298 gwlarson 2.50 setview(&ourview);
299 greg 2.8 prvr = fbuf; /* mark for renaming */
300     }
301 greg 2.18 if (pout != NULL & prvr != NULL) {
302 greg 2.8 sprintf(fbuf, pout, seq);
303 greg 2.18 if (!strcmp(prvr, fbuf)) { /* rename */
304     strcpy(fbuf2, fbuf);
305     for (cp = fbuf2; *cp; cp++)
306     ;
307     while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
308     cp--;
309     strcpy(cp, RFTEMPLATE);
310 greg 2.8 prvr = mktemp(fbuf2);
311 greg 2.28 if (rename(fbuf, prvr) < 0)
312     if (errno == ENOENT) { /* ghost file */
313     sprintf(errmsg,
314     "new output file \"%s\"",
315     fbuf);
316     error(WARNING, errmsg);
317     prvr = NULL;
318     } else { /* serious error */
319     sprintf(errmsg,
320 greg 2.8 "cannot rename \"%s\" to \"%s\"",
321     fbuf, prvr);
322 greg 2.28 error(SYSTEM, errmsg);
323     }
324 greg 2.8 }
325     }
326 greg 2.26 npicts = 0; /* render sequence */
327 greg 2.8 do {
328     if (seq && nextview(stdin) == EOF)
329     break;
330 greg 2.25 pctdone = 0.0;
331 greg 2.8 if (pout != NULL) {
332     sprintf(fbuf, pout, seq);
333 greg 2.27 if (file_exists(fbuf)) {
334     if (prvr != NULL || !strcmp(fbuf, pout)) {
335     sprintf(errmsg,
336     "output file \"%s\" exists",
337     fbuf);
338     error(USER, errmsg);
339     }
340 gwlarson 2.50 setview(&ourview);
341 greg 2.26 continue; /* don't clobber */
342 greg 2.27 }
343 greg 2.8 if (freopen(fbuf, "w", stdout) == NULL) {
344     sprintf(errmsg,
345     "cannot open output file \"%s\"", fbuf);
346     error(SYSTEM, errmsg);
347     }
348 greg 2.17 #ifdef MSDOS
349     setmode(fileno(stdout), O_BINARY);
350     #endif
351 greg 2.8 dupheader();
352     }
353     hres = hresolu; vres = vresolu; pa = pixaspect;
354     if (prvr != NULL)
355     if (viewfile(prvr, &ourview, &rs) <= 0
356 greg 2.52 || rs.rt != PIXSTANDARD) {
357 greg 2.8 sprintf(errmsg,
358     "cannot recover view parameters from \"%s\"", prvr);
359     error(WARNING, errmsg);
360     } else {
361     pa = 0.0;
362     hres = scanlen(&rs);
363     vres = numscans(&rs);
364     }
365 greg 2.9 if ((cp = setview(&ourview)) != NULL)
366     error(USER, cp);
367 greg 2.8 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
368     if (seq) {
369     if (ralrm > 0) {
370 greg 2.11 fprintf(stderr, "FRAME %d:", seq);
371     fprintview(&ourview, stderr);
372     putc('\n', stderr);
373     fflush(stderr);
374 greg 2.8 }
375     printf("FRAME=%d\n", seq);
376     }
377     fputs(VIEWSTR, stdout);
378     fprintview(&ourview, stdout);
379     putchar('\n');
380     if (pa < .99 || pa > 1.01)
381     fputaspect(pa, stdout);
382     fputformat(COLRFMT, stdout);
383     putchar('\n');
384     if (zout != NULL)
385 greg 2.9 sprintf(cp=fbuf, zout, seq);
386 greg 2.8 else
387 greg 2.9 cp = NULL;
388     render(cp, prvr);
389 greg 2.8 prvr = NULL;
390 greg 2.26 npicts++;
391 greg 2.8 } while (seq++);
392 greg 2.26 /* check that we did something */
393     if (npicts == 0)
394     error(WARNING, "no output produced");
395 greg 2.8 }
396    
397    
398     nextview(fp) /* get next view from fp */
399     FILE *fp;
400     {
401 greg 2.9 char linebuf[256];
402 greg 2.8
403 gwlarson 2.49 copystruct(&lastview, &ourview);
404 greg 2.8 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
405 greg 2.9 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
406 greg 2.8 return(0);
407     return(EOF);
408     }
409    
410    
411 greg 1.11 render(zfile, oldfile) /* render the scene */
412     char *zfile, *oldfile;
413 greg 1.1 {
414     COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
415 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
416 greg 2.2 char *sampdens; /* previous sample density */
417 greg 1.1 int ypos; /* current scanline */
418 greg 1.15 int ystep; /* current y step size */
419 greg 1.33 int hstep; /* h step size */
420 greg 1.16 int zfd;
421 greg 1.1 COLOR *colptr;
422 greg 1.11 float *zptr;
423 greg 1.1 register int i;
424 greg 2.52 /* check for empty image */
425     if (hres <= 0 || vres <= 0) {
426     error(WARNING, "empty output picture");
427     fprtresolu(0, 0, stdout);
428     return;
429     }
430 greg 1.9 /* allocate scanlines */
431 greg 1.1 for (i = 0; i <= psample; i++) {
432 greg 2.8 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
433 greg 1.1 if (scanbar[i] == NULL)
434 greg 1.11 goto memerr;
435 greg 1.1 }
436 greg 2.2 hstep = (psample*140+49)/99; /* quincunx sampling */
437     ystep = (psample*99+70)/140;
438     if (hstep > 2) {
439 greg 2.8 i = hres/hstep + 2;
440 greg 2.52 if ((sampdens = (char *)malloc(i)) == NULL)
441 greg 2.2 goto memerr;
442     while (i--)
443     sampdens[i] = hstep;
444     } else
445     sampdens = NULL;
446 greg 2.26 /* open z-file */
447 greg 1.11 if (zfile != NULL) {
448 greg 1.16 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
449 greg 2.26 sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
450 greg 1.11 error(SYSTEM, errmsg);
451     }
452 greg 2.17 #ifdef MSDOS
453     setmode(zfd, O_BINARY);
454     #endif
455 greg 1.11 for (i = 0; i <= psample; i++) {
456 greg 2.8 zbar[i] = (float *)malloc(hres*sizeof(float));
457 greg 1.11 if (zbar[i] == NULL)
458     goto memerr;
459     }
460     } else {
461 greg 1.16 zfd = -1;
462 greg 1.11 for (i = 0; i <= psample; i++)
463     zbar[i] = NULL;
464     }
465 greg 1.1 /* write out boundaries */
466 greg 2.8 fprtresolu(hres, vres, stdout);
467 greg 1.10 /* recover file and compute first */
468 greg 1.11 i = salvage(oldfile);
469 greg 2.42 if (i >= vres)
470     goto alldone;
471 greg 1.23 if (zfd != -1 && i > 0 &&
472 greg 2.52 lseek(zfd, (off_t)i*hres*sizeof(float), 0) < 0)
473 greg 2.26 error(SYSTEM, "z-file seek error in render");
474 greg 2.8 pctdone = 100.0*i/vres;
475 greg 1.24 if (ralrm > 0) /* report init stats */
476     report();
477 greg 2.14 #ifndef BSD
478 greg 1.32 else
479     #endif
480 greg 2.22 signal(SIGCONT, report);
481 greg 2.47 ypos = vres-1 - i; /* initialize sampling */
482 gregl 2.48 if (directvis)
483     init_drawsources(psample);
484 greg 2.8 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
485 greg 1.10 /* compute scanlines */
486 greg 1.15 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
487     /* bottom adjust? */
488     if (ypos < 0) {
489     ystep += ypos;
490     ypos = 0;
491     }
492     colptr = scanbar[ystep]; /* move base to top */
493     scanbar[ystep] = scanbar[0];
494 greg 1.1 scanbar[0] = colptr;
495 greg 1.15 zptr = zbar[ystep];
496     zbar[ystep] = zbar[0];
497 greg 1.11 zbar[0] = zptr;
498 greg 1.10 /* fill base line */
499 greg 2.2 fillscanline(scanbar[0], zbar[0], sampdens,
500 greg 2.8 hres, ypos, hstep);
501 greg 1.10 /* fill bar */
502 greg 2.8 fillscanbar(scanbar, zbar, hres, ypos, ystep);
503 gregl 2.48 if (directvis) /* add bitty sources */
504     drawsources(scanbar, zbar, 0, hres, ypos, ystep);
505 greg 1.10 /* write it out */
506 greg 2.14 #ifndef BSD
507 greg 2.22 signal(SIGCONT, SIG_IGN); /* don't interrupt writes */
508 greg 1.32 #endif
509 greg 1.15 for (i = ystep; i > 0; i--) {
510 greg 1.17 if (zfd != -1 && write(zfd, (char *)zbar[i],
511 greg 2.8 hres*sizeof(float))
512     < hres*sizeof(float))
513 greg 1.1 goto writerr;
514 greg 2.8 if (fwritescan(scanbar[i], hres, stdout) < 0)
515 greg 1.11 goto writerr;
516     }
517 greg 1.1 if (fflush(stdout) == EOF)
518     goto writerr;
519 greg 1.24 /* record progress */
520 greg 2.8 pctdone = 100.0*(vres-1-ypos)/vres;
521 greg 2.32 if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
522 greg 1.24 report();
523 greg 2.14 #ifndef BSD
524 greg 1.32 else
525 greg 2.22 signal(SIGCONT, report);
526 greg 1.32 #endif
527 greg 1.1 }
528 greg 1.11 /* clean up */
529 greg 2.22 signal(SIGCONT, SIG_IGN);
530 greg 2.42 if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
531     < hres*sizeof(float))
532     goto writerr;
533     fwritescan(scanbar[0], hres, stdout);
534     if (fflush(stdout) == EOF)
535     goto writerr;
536     alldone:
537 greg 1.16 if (zfd != -1) {
538 greg 1.20 if (close(zfd) == -1)
539     goto writerr;
540 greg 1.11 for (i = 0; i <= psample; i++)
541 greg 2.52 free((void *)zbar[i]);
542 greg 1.11 }
543 greg 1.1 for (i = 0; i <= psample; i++)
544 greg 2.52 free((void *)scanbar[i]);
545 greg 2.2 if (sampdens != NULL)
546     free(sampdens);
547 greg 1.11 pctdone = 100.0;
548 greg 2.13 if (ralrm > 0)
549     report();
550 greg 2.23 signal(SIGCONT, SIG_DFL);
551 greg 1.1 return;
552     writerr:
553     error(SYSTEM, "write error in render");
554 greg 1.11 memerr:
555     error(SYSTEM, "out of memory in render");
556 greg 1.1 }
557    
558    
559 greg 2.2 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
560 greg 2.14 register COLOR *scanline;
561     register float *zline;
562 greg 2.2 register char *sd;
563 greg 1.9 int xres, y, xstep;
564 greg 1.1 {
565 greg 1.33 static int nc = 0; /* number of calls */
566 greg 2.2 int bl = xstep, b = xstep;
567 greg 2.14 double z;
568 greg 1.1 register int i;
569 greg 2.45
570 greg 1.11 z = pixvalue(scanline[0], 0, y);
571     if (zline) zline[0] = z;
572 greg 1.33 /* zig-zag start for quincunx pattern */
573 greg 2.2 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
574 greg 1.15 if (i >= xres) {
575     xstep += xres-1-i;
576     i = xres-1;
577     }
578 greg 1.11 z = pixvalue(scanline[i], i, y);
579     if (zline) zline[i] = z;
580 greg 2.2 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
581 greg 2.3 if (i <= xstep)
582     b = fillsample(scanline, zline, 0, y, i, 0, b/2);
583     else
584     b = fillsample(scanline+i-xstep,
585 greg 2.38 zline ? zline+i-xstep : (float *)NULL,
586 greg 2.3 i-xstep, y, xstep, 0, b/2);
587 greg 2.2 if (sd) *sd++ = nc & 1 ? bl : b;
588     bl = b;
589 greg 1.1 }
590 greg 2.2 if (sd && nc & 1) *sd = bl;
591 greg 1.1 }
592    
593    
594 greg 1.11 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
595 greg 2.14 register COLOR *scanbar[];
596     register float *zbar[];
597 greg 1.9 int xres, y, ysize;
598 greg 1.1 {
599     COLOR vline[MAXDIV+1];
600 greg 1.11 float zline[MAXDIV+1];
601 greg 1.1 int b = ysize;
602     register int i, j;
603 greg 2.45
604 greg 1.8 for (i = 0; i < xres; i++) {
605 greg 1.1 copycolor(vline[0], scanbar[0][i]);
606     copycolor(vline[ysize], scanbar[ysize][i]);
607 greg 1.11 if (zbar[0]) {
608     zline[0] = zbar[0][i];
609     zline[ysize] = zbar[ysize][i];
610     }
611 greg 2.38 b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
612 greg 1.11 i, y, 0, ysize, b/2);
613 greg 2.45
614 greg 1.1 for (j = 1; j < ysize; j++)
615     copycolor(scanbar[j][i], vline[j]);
616 greg 1.11 if (zbar[0])
617     for (j = 1; j < ysize; j++)
618     zbar[j][i] = zline[j];
619 greg 1.1 }
620     }
621    
622    
623     int
624 greg 2.14 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
625     register COLOR *colline;
626     register float *zline;
627 greg 1.1 int x, y;
628     int xlen, ylen;
629     int b;
630     {
631 greg 2.14 double ratio;
632     double z;
633 greg 1.1 COLOR ctmp;
634     int ncut;
635     register int len;
636 greg 2.45
637 greg 1.1 if (xlen > 0) /* x or y length is zero */
638     len = xlen;
639     else
640     len = ylen;
641 greg 2.45
642 greg 1.1 if (len <= 1) /* limit recursion */
643     return(0);
644 greg 2.45
645     if (b > 0 ||
646     (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
647 greg 1.11 || bigdiff(colline[0], colline[len], maxdiff)) {
648 greg 2.45
649 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
650     if (zline) zline[len>>1] = z;
651 greg 1.1 ncut = 1;
652     } else { /* interpolate */
653     copycolor(colline[len>>1], colline[len]);
654     ratio = (double)(len>>1) / len;
655     scalecolor(colline[len>>1], ratio);
656 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
657     ratio = 1.0 - ratio;
658 greg 1.1 copycolor(ctmp, colline[0]);
659     scalecolor(ctmp, ratio);
660     addcolor(colline[len>>1], ctmp);
661 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
662 greg 1.1 ncut = 0;
663     }
664     /* recurse */
665 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
666 greg 2.45
667 greg 2.38 ncut += fillsample(colline+(len>>1),
668     zline ? zline+(len>>1) : (float *)NULL,
669 greg 1.11 x+(xlen>>1), y+(ylen>>1),
670     xlen-(xlen>>1), ylen-(ylen>>1), b/2);
671 greg 1.1
672     return(ncut);
673     }
674    
675    
676 greg 1.11 double
677 greg 1.1 pixvalue(col, x, y) /* compute pixel value */
678     COLOR col; /* returned color */
679     int x, y; /* pixel position */
680     {
681 gwlarson 2.49 RAY thisray;
682     FVECT lorg, ldir;
683     double hpos, vpos, lmax, d;
684     /* compute view ray */
685     hpos = (x+pixjitter())/hres;
686     vpos = (y+pixjitter())/vres;
687     if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
688     &ourview, hpos, vpos)) < -FTINY) {
689 greg 1.22 setcolor(col, 0.0, 0.0, 0.0);
690     return(0.0);
691     }
692 greg 1.1
693 gwlarson 2.49 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
694    
695     /* optional motion blur */
696     if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
697     &lastview, hpos, vpos)) >= -FTINY) {
698     register int i;
699     register double d = mblur*(.5-urand(281+samplendx));
700    
701     thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
702     for (i = 3; i--; ) {
703     thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i];
704     thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i];
705     }
706     if (normalize(thisray.rdir) == 0.0)
707     return(0.0);
708     }
709    
710 greg 1.1 rayorigin(&thisray, NULL, PRIMARY, 1.0);
711 greg 1.25
712 greg 1.1 rayvalue(&thisray); /* trace ray */
713    
714     copycolor(col, thisray.rcol); /* return color */
715 greg 2.45
716 greg 1.20 return(thisray.rt); /* return distance */
717 greg 1.1 }
718    
719    
720     int
721     salvage(oldfile) /* salvage scanlines from killed program */
722     char *oldfile;
723     {
724     COLR *scanline;
725     FILE *fp;
726     int x, y;
727    
728     if (oldfile == NULL)
729 greg 2.37 goto gotzip;
730    
731 greg 1.9 if ((fp = fopen(oldfile, "r")) == NULL) {
732 greg 1.1 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
733     error(WARNING, errmsg);
734 greg 2.37 goto gotzip;
735 greg 1.1 }
736 greg 2.17 #ifdef MSDOS
737     setmode(fileno(fp), O_BINARY);
738     #endif
739 greg 1.1 /* discard header */
740 greg 2.19 getheader(fp, NULL, NULL);
741 greg 1.1 /* get picture size */
742 greg 1.36 if (!fscnresolu(&x, &y, fp)) {
743 greg 2.29 sprintf(errmsg, "bad recover file \"%s\" - not removed",
744     oldfile);
745 greg 1.2 error(WARNING, errmsg);
746 greg 1.1 fclose(fp);
747 greg 2.37 goto gotzip;
748 greg 1.1 }
749    
750 greg 2.8 if (x != hres || y != vres) {
751 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
752     oldfile);
753     error(USER, errmsg);
754     }
755    
756 greg 2.8 scanline = (COLR *)malloc(hres*sizeof(COLR));
757 greg 1.1 if (scanline == NULL)
758     error(SYSTEM, "out of memory in salvage");
759 greg 2.8 for (y = 0; y < vres; y++) {
760     if (freadcolrs(scanline, hres, fp) < 0)
761 greg 1.1 break;
762 greg 2.8 if (fwritecolrs(scanline, hres, stdout) < 0)
763 greg 1.1 goto writerr;
764     }
765     if (fflush(stdout) == EOF)
766     goto writerr;
767 greg 2.52 free((void *)scanline);
768 greg 1.1 fclose(fp);
769     unlink(oldfile);
770     return(y);
771 greg 2.37 gotzip:
772     if (fflush(stdout) == EOF)
773     error(SYSTEM, "error writing picture header");
774     return(0);
775 greg 1.1 writerr:
776 greg 2.9 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
777     error(SYSTEM, errmsg);
778 greg 1.31 }
779    
780    
781     int
782     pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
783     register int x, y;
784     int xres, yres;
785     {
786     x -= y;
787     while (x < 0)
788     x += xres;
789     return((((x>>2)*yres + y) << 2) + (x & 3));
790 greg 1.1 }