ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.107
Committed: Sat Jan 25 04:57:56 2025 UTC (3 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.106: +2 -1 lines
Log Message:
fix(rpict): Fixed issue with -pm corrupting first image in sequence

File Contents

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