ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.89
Committed: Tue Feb 24 19:39:27 2015 UTC (9 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.88: +17 -6 lines
Log Message:
Initial check-in of photon map addition by Roland Schregle

File Contents

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