ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.55
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.54: +7 -14 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

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