ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.71
Committed: Sat Oct 23 18:55:53 2004 UTC (19 years, 6 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1
Changes since 2.70: +2 -2 lines
Log Message:
Compatibility fixes as suggested by Siegbert Debatin.

File Contents

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