ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimove1.c
Revision: 3.17
Committed: Fri May 20 02:06:39 2011 UTC (12 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R1
Changes since 3.16: +12 -12 lines
Log Message:
Changed every instance of BYTE to uby8 to avoid conflicts

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ranimove1.c,v 3.16 2010/11/05 04:36:24 greg Exp $";
3 #endif
4 /*
5 * ranimove1.c
6 *
7 * Basic frame rendering routines for ranimove(1).
8 *
9 * Created by Gregory Ward on Wed Jan 08 2003.
10 */
11
12 #include "copyright.h"
13
14 #include <string.h>
15
16 #include "platform.h"
17 #include "ranimove.h"
18 #include "otypes.h"
19 #include "source.h"
20 #include "random.h"
21
22 double acctab[256]; /* accuracy value table */
23
24 int hres, vres; /* frame resolution (fcur) */
25 double pixaspect; /* pixel aspect ratio */
26
27 VIEW vw; /* view for this frame */
28 COLOR *cbuffer; /* color at each pixel */
29 float *zbuffer; /* depth at each pixel */
30 OBJECT *obuffer; /* object id at each pixel */
31 short *xmbuffer; /* x motion at each pixel */
32 short *ymbuffer; /* y motion at each pixel */
33 uby8 *abuffer; /* accuracy at each pixel */
34 uby8 *sbuffer; /* sample count per pixel */
35
36 VIEW vwprev; /* last frame's view */
37 COLOR *cprev; /* last frame colors */
38 float *zprev; /* last frame depth */
39 OBJECT *oprev; /* last frame objects */
40 uby8 *aprev; /* last frame accuracy */
41
42 float *cerrmap; /* conspicuous error map */
43 COLOR *val2map; /* value-squared map for variance */
44
45 double frm_stop; /* when to stop rendering this frame */
46
47 double hlsmax; /* maximum high-level saliency this frame */
48
49
50 static void next_frame(void);
51 static int sample_here(int x, int y);
52 static int offset_cmp(const void *p1, const void *p2);
53 static void setmotion(int n, FVECT wpos);
54 static void init_frame_sample(void);
55
56
57 #if 0
58 extern void
59 write_map( /* write out float map (debugging) */
60 float *mp,
61 char *fn
62 )
63 {
64 FILE *fp = fopen(fn, "w");
65 COLOR scanbuf[2048];
66 int x, y;
67
68 if (fp == NULL)
69 return;
70 newheader("RADIANCE", fp);
71 fputformat(COLRFMT, fp);
72 fputc('\n', fp); /* end header */
73 fprtresolu(hres, vres, fp);
74 for (y = vres; y--; ) { /* write scanlines */
75 float *bp = mp + (y+1)*hres - 1;
76 for (x = hres; x--; bp--)
77 setcolor(scanbuf[x], *bp, *bp, *bp);
78 if (fwritescan(scanbuf, hres, fp) < 0)
79 break;
80 }
81 fclose(fp);
82 }
83 #endif
84
85
86 static void
87 next_frame(void) /* prepare next frame buffer */
88 {
89 VIEW *fv;
90 char *err;
91 /* get previous view */
92 if (vw.type != 0)
93 vwprev = vw;
94 else if (fcur > 1 && (fv = getview(fcur-1)) != NULL) {
95 vwprev = *fv;
96 if (setview(&vwprev) != NULL)
97 vwprev.type = 0;
98 }
99 /* get current view */
100 if ((fv = getview(fcur)) == NULL) {
101 sprintf(errmsg, "cannot get view for frame %d", fcur);
102 error(USER, errmsg);
103 }
104 vw = *fv;
105 if ((err = setview(&vw)) != NULL) {
106 sprintf(errmsg, "view error at frame %d: %s", fcur, err);
107 error(USER, errmsg);
108 }
109 if (cbuffer == NULL) {
110 int n;
111 /* compute resolution and allocate */
112 switch (sscanf(vval(RESOLUTION), "%d %d %lf",
113 &hres, &vres, &pixaspect)) {
114 case 1:
115 vres = hres;
116 /* fall through */
117 case 2:
118 pixaspect = 1.;
119 /* fall through */
120 case 3:
121 if ((hres > 0) & (vres > 0))
122 break;
123 /* fall through */
124 default:
125 sprintf(errmsg, "bad %s value", vnam(RESOLUTION));
126 error(USER, errmsg);
127 }
128 normaspect(viewaspect(&vw), &pixaspect, &hres, &vres);
129 cbuffer = (COLOR *)malloc(sizeof(COLOR)*hres*vres);
130 zbuffer = (float *)malloc(sizeof(float)*hres*vres);
131 obuffer = (OBJECT *)malloc(sizeof(OBJECT)*hres*vres);
132 xmbuffer = (short *)malloc(sizeof(short)*hres*vres);
133 ymbuffer = (short *)malloc(sizeof(short)*hres*vres);
134 abuffer = (uby8 *)calloc(hres*vres, sizeof(uby8));
135 sbuffer = (uby8 *)calloc(hres*vres, sizeof(uby8));
136 cprev = (COLOR *)malloc(sizeof(COLOR)*hres*vres);
137 zprev = (float *)malloc(sizeof(float)*hres*vres);
138 oprev = (OBJECT *)malloc(sizeof(OBJECT)*hres*vres);
139 aprev = (uby8 *)malloc(sizeof(uby8)*hres*vres);
140 if ((cbuffer==NULL) | (zbuffer==NULL) | (obuffer==NULL) |
141 (xmbuffer==NULL) | (ymbuffer==NULL) |
142 (abuffer==NULL) | (sbuffer==NULL) |
143 (cprev==NULL) | (zprev == NULL) |
144 (oprev==NULL) | (aprev==NULL))
145 error(SYSTEM, "out of memory in init_frame");
146 for (n = hres*vres; n--; ) {
147 zprev[n] = -1.f;
148 oprev[n] = OVOID;
149 }
150 frm_stop = getTime() + rtperfrm;
151 } else {
152 COLOR *cp; /* else just swap buffers */
153 float *fp;
154 OBJECT *op;
155 uby8 *bp;
156 cp = cprev; cprev = cbuffer; cbuffer = cp;
157 fp = zprev; zprev = zbuffer; zbuffer = fp;
158 op = oprev; oprev = obuffer; obuffer = op;
159 bp = aprev; aprev = abuffer; abuffer = bp;
160 memset(abuffer, '\0', sizeof(uby8)*hres*vres);
161 memset(sbuffer, '\0', sizeof(uby8)*hres*vres);
162 frm_stop += rtperfrm;
163 }
164 cerrmap = NULL;
165 val2map = NULL;
166 }
167
168
169 #define SAMPDIST 3 /* Maximum distance to neighbor sample */
170 #define SAMPDIST2 (SAMPDIST*SAMPDIST)
171
172
173 static int
174 sample_here( /* 4x4 quincunx sample at this pixel? */
175 register int x,
176 register int y
177 )
178 {
179 if (y & 0x1) /* every other row has samples */
180 return(0);
181 if (y & 0x3) /* every fourth row is offset */
182 x += 2;
183 return((x & 0x3) == 0); /* every fourth column is sampled */
184 }
185
186
187 extern void
188 sample_pos( /* compute jittered sample position */
189 double hv[2],
190 int x,
191 int y,
192 int sn
193 )
194 {
195 int hl[2];
196
197 hl[0] = x; hl[1] = y;
198 multisamp(hv, 2, urand(ilhash(hl,2) + sn));
199 hv[0] = ((double)x + hv[0]) / (double)hres;
200 hv[1] = ((double)y + hv[1]) / (double)vres;
201 }
202
203
204 extern double
205 sample_wt( /* compute interpolant sample weight */
206 int xo,
207 int yo
208 )
209 {
210 static double etab[400];
211 /* we can't use the name rad2 here, for some reason Visual C
212 thinks that is a constant (compiler bug?) */
213 int rad_2 = xo*xo + yo*yo;
214 int i;
215
216 if (etab[0] <= FTINY) /* initialize exponent table */
217 for (i = 400; i--; )
218 etab[i] = exp(-0.1*i);
219
220 /* look up Gaussian */
221 i = (int)((10.*3./(double)SAMPDIST2)*rad_2 + .5);
222 if (i >= 400)
223 return(0.0);
224 return(etab[i]);
225 }
226
227
228 static int
229 offset_cmp( /* compare offset distances */
230 const void *p1,
231 const void *p2
232 )
233 {
234 return(*(const int *)p1 - *(const int *)p2);
235 }
236
237
238 extern int
239 getclosest( /* get nc closest neighbors on same object */
240 int *iarr,
241 int nc,
242 int x,
243 int y
244 )
245 {
246 #define NSCHECK ((2*SAMPDIST+1)*(2*SAMPDIST+1))
247 static int hro, vro;
248 static int ioffs[NSCHECK];
249 OBJECT myobj;
250 int i0, nf;
251 register int i, j;
252 /* get our object number */
253 myobj = obuffer[fndx(x, y)];
254 /* special case for borders */
255 if ((x < SAMPDIST) | (x >= hres-SAMPDIST) |
256 (y < SAMPDIST) | (y >= vres-SAMPDIST)) {
257 int tndx[NSCHECK][2];
258 nf = 0;
259 for (j = y - SAMPDIST; j <= y + SAMPDIST; j++) {
260 if (j >= vres) break;
261 if (j < 0) j = 0;
262 for (i = x - SAMPDIST; i <= x + SAMPDIST; i++) {
263 if (i >= hres) break;
264 if (i < 0) i = 0;
265 i0 = fndx(i, j);
266 if (!sbuffer[i0])
267 continue;
268 if ((myobj != OVOID) & (obuffer[i0] != myobj))
269 continue;
270 tndx[nf][0] = (i-x)*(i-x) + (j-y)*(j-y);
271 tndx[nf][1] = i0;
272 nf++;
273 }
274 }
275 qsort((void *)tndx, nf, 2*sizeof(int), offset_cmp);
276 if (nf > nc)
277 nf = nc;
278 for (i = nf; i--; )
279 iarr[i] = tndx[i][1];
280 return(nf);
281 }
282 /* initialize offset array */
283 if ((hres != hro) | (vres != vro)) {
284 int toffs[NSCHECK][2];
285 i0 = fndx(SAMPDIST, SAMPDIST);
286 nf = 0;
287 for (i = 0; i <= 2*SAMPDIST; i++)
288 for (j = 0; j <= 2*SAMPDIST; j++) {
289 toffs[nf][0] = (i-SAMPDIST)*(i-SAMPDIST) +
290 (j-SAMPDIST)*(j-SAMPDIST);
291 toffs[nf][1] = fndx(i, j) - i0;
292 nf++;
293 }
294 qsort((void *)toffs, nf, 2*sizeof(int), offset_cmp);
295 for (i = NSCHECK; i--; )
296 ioffs[i] = toffs[i][1];
297 hro = hres;
298 vro = vres;
299 }
300 /* find up to nc neighbors */
301 i0 = fndx(x, y);
302 for (j = 0, nf = 0; (j < NSCHECK) & (nf < nc); j++) {
303 i = i0 + ioffs[j];
304 if (sbuffer[i] && (myobj == OVOID) | (obuffer[i] == myobj))
305 iarr[nf++] = i;
306 }
307 /* return number found */
308 return(nf);
309 #undef NSCHECK
310 }
311
312
313 static void
314 setmotion( /* compute motion vector for this pixel */
315 register int n,
316 FVECT wpos
317 )
318 {
319 FVECT ovp;
320 int moi;
321 int xp, yp;
322 /* ID object and update maximum HLS */
323 moi = getmove(obuffer[n]);
324 if (moi >= 0 && obj_move[moi].cprio > hlsmax)
325 hlsmax = obj_move[moi].cprio;
326 if (vwprev.type == 0) /* return leaves MO_UNK */
327 return;
328 if (moi >= 0) { /* move object point back */
329 multp3(ovp, wpos, obj_move[moi].bxfm);
330 wpos = ovp;
331 }
332 viewloc(ovp, &vwprev, wpos);
333 if (ovp[2] <= FTINY)
334 return;
335 xp = (int)(ovp[0]*hres);
336 yp = (int)(ovp[1]*vres);
337 xmbuffer[n] = xp - (n % hres);
338 ymbuffer[n] = yp - (n / hres);
339 if ((xp < 0) | (xp >= hres))
340 return;
341 if ((yp < 0) | (yp >= vres))
342 return;
343 n = fndx(xp, yp);
344 if ((zprev[n] < 0.97*ovp[2]) | (zprev[n] > 1.03*ovp[2]))
345 oprev[n] = OVOID; /* assume it's a bad match */
346 }
347
348
349 static void
350 init_frame_sample(void) /* sample our initial frame */
351 {
352 RAY ir;
353 int x, y;
354 register int n;
355
356 if (!silent) {
357 printf("\tComputing initial samples...");
358 fflush(stdout);
359 }
360 hlsmax = CSF_SMN;
361 for (y = vres; y--; )
362 for (x = hres; x--; ) {
363 double hv[2];
364 n = fndx(x, y);
365 xmbuffer[n] = MO_UNK;
366 ymbuffer[n] = MO_UNK;
367 sample_pos(hv, x, y, 0);
368 ir.rmax = viewray(ir.rorg, ir.rdir, &vw, hv[0], hv[1]);
369 if (ir.rmax < -FTINY) {
370 setcolor(cbuffer[n], 0., 0., 0.);
371 zbuffer[n] = FHUGE;
372 obuffer[n] = OVOID;
373 abuffer[n] = ADISTANT;
374 continue;
375 }
376 if (!sample_here(x, y)) { /* just cast */
377 rayorigin(&ir, PRIMARY, NULL, NULL);
378 if (!localhit(&ir, &thescene)) {
379 if (ir.ro != &Aftplane && sourcehit(&ir)) {
380 rayshade(&ir, ir.ro->omod);
381 rayparticipate(&ir);
382 }
383 copycolor(cbuffer[n], ir.rcol);
384 zbuffer[n] = ir.rot;
385 obuffer[n] = ir.robj;
386 abuffer[n] = ADISTANT;
387 sbuffer[n] = 1;
388 } else {
389 zbuffer[n] = ir.rot;
390 obuffer[n] = ir.robj;
391 setmotion(n, ir.rop);
392 }
393 continue;
394 }
395 if (nprocs > 1) { /* get sample */
396 int rval;
397 rayorigin(&ir, PRIMARY, NULL, NULL);
398 ir.rno = n;
399 rval = ray_pqueue(&ir);
400 if (!rval)
401 continue;
402 if (rval < 0)
403 quit(1);
404 n = ir.rno;
405 } else
406 ray_trace(&ir);
407 copycolor(cbuffer[n], ir.rcol);
408 zbuffer[n] = ir.rot;
409 obuffer[n] = ir.robj;
410 sbuffer[n] = 1;
411 if (ir.rot >= 0.99*FHUGE)
412 abuffer[n] = ADISTANT;
413 else {
414 abuffer[n] = ALOWQ;
415 setmotion(n, ir.rop);
416 }
417 }
418 if (nprocs > 1) /* get stragglers */
419 while (ray_presult(&ir, 0)) {
420 n = ir.rno;
421 copycolor(cbuffer[n], ir.rcol);
422 zbuffer[n] = ir.rot;
423 obuffer[n] = ir.robj;
424 sbuffer[n] = 1;
425 if (ir.rot >= FHUGE)
426 abuffer[n] = ADISTANT;
427 else {
428 abuffer[n] = ALOWQ;
429 setmotion(n, ir.rop);
430 }
431 }
432 /* ambiguate object boundaries */
433 for (y = vres-1; y--; )
434 for (x = hres-1; x--; ) {
435 OBJECT obj;
436 n = fndx(x, y);
437 if ((obj = obuffer[n]) == OVOID)
438 continue;
439 if ((obuffer[n+1] != OVOID) & (obuffer[n+1] != obj)) {
440 obuffer[n] = OVOID;
441 obuffer[n+1] = OVOID;
442 }
443 if ((obuffer[n+hres] != OVOID) & (obuffer[n+hres] != obj)) {
444 obuffer[n] = OVOID;
445 obuffer[n+hres] = OVOID;
446 }
447 }
448
449 if (!silent)
450 printf("done\n");
451 }
452
453
454 extern int
455 getambcolor( /* get ambient color for object if we can */
456 COLOR clr,
457 int obj
458 )
459 {
460 register OBJREC *op;
461
462 if (obj == OVOID)
463 return(0);
464 op = objptr(obj); /* search for material */
465 if (op->omod == OVOID)
466 return(0);
467 op = findmaterial(objptr(op->omod));
468 if (op == NULL)
469 return(0);
470 /*
471 * Since this routine is called to compute the difference
472 * from rendering with and without interreflections,
473 * we don't want to return colors for materials that are
474 * explicitly excluded from the HQ ambient calculation.
475 */
476 if (hirendparams.ambincl >= 0) {
477 int i;
478 char *lv;
479 for (i = 0; (lv = rpambmod(&hirendparams,i)) != NULL; i++)
480 if (lv[0] == op->oname[0] &&
481 !strcmp(lv+1, op->oname+1))
482 break;
483 if ((lv != NULL) ^ hirendparams.ambincl)
484 return(0);
485 }
486 switch (op->otype) {
487 case MAT_PLASTIC:
488 case MAT_METAL:
489 case MAT_PLASTIC2:
490 case MAT_METAL2:
491 case MAT_PFUNC:
492 case MAT_MFUNC:
493 case MAT_PDATA:
494 case MAT_MDATA:
495 case MAT_TRANS:
496 case MAT_TRANS2:
497 case MAT_TFUNC:
498 case MAT_TDATA:
499 if (op->oargs.nfargs < 3)
500 return(0);
501 setcolor(clr, op->oargs.farg[0], op->oargs.farg[1],
502 op->oargs.farg[2]);
503 return(1);
504 case MAT_BRTDF:
505 if (op->oargs.nfargs < 6)
506 return(0);
507 setcolor(clr, op->oargs.farg[0]+op->oargs.farg[3],
508 op->oargs.farg[1]+op->oargs.farg[4],
509 op->oargs.farg[2]+op->oargs.farg[5]);
510 scalecolor(clr, 0.5);
511 return(1);
512 case MAT_LIGHT:
513 case MAT_GLOW:
514 case MAT_ILLUM:
515 setcolor(clr, 0., 0., 0.);
516 return(1);
517 }
518 return(0);
519 }
520
521
522 extern double
523 estimaterr( /* estimate relative error from samples */
524 COLOR cs,
525 COLOR cs2,
526 int ns,
527 int ns0
528 )
529 {
530 double d, d2, brt;
531
532 if (ns <= 1 || (brt = bright(cs)/ns) < 1e-14)
533 return(1.0);
534 /* use largest of RGB std. dev. */
535 d2 = colval(cs2,RED) - colval(cs,RED)*colval(cs,RED)/ns;
536 d = colval(cs2,GRN) - colval(cs,GRN)*colval(cs,GRN)/ns;
537 if (d > d2) d2 = d;
538 d = colval(cs2,BLU) - colval(cs,BLU)*colval(cs,BLU)/ns;
539 if (d > d2) d2 = d;
540 /* use s.d. if <= 1 central sample */
541 if (ns0 <= 1)
542 return(sqrt(d2/(ns-1))/brt);
543 /* use s.d./sqrt(ns0) otherwise */
544 return(sqrt(d2/((ns-1)*ns0))/brt);
545 }
546
547
548 extern double
549 comperr( /* estimate relative error in neighborhood */
550 int *neigh,
551 int nc,
552 int ns0
553 )
554 {
555 COLOR csum, csum2;
556 COLOR ctmp;
557 int i;
558 int ns;
559 register int n;
560 /* add together samples */
561 setcolor(csum, 0., 0., 0.);
562 setcolor(csum2, 0., 0., 0.);
563 for (i = 0, ns = 0; (i < nc) & (ns < NSAMPOK); i++) {
564 n = neigh[i];
565 addcolor(csum, cbuffer[n]);
566 if (val2map != NULL) {
567 addcolor(csum2, val2map[n]);
568 ns += sbuffer[n];
569 continue;
570 }
571 if (sbuffer[n] != 1)
572 error(CONSISTENCY, "bad count in comperr");
573 setcolor(ctmp,
574 colval(cbuffer[n],RED)*colval(cbuffer[n],RED),
575 colval(cbuffer[n],GRN)*colval(cbuffer[n],GRN),
576 colval(cbuffer[n],BLU)*colval(cbuffer[n],BLU));
577 addcolor(csum2, ctmp);
578 ns++;
579 }
580 return(estimaterr(csum, csum2, ns, ns0));
581 }
582
583
584 extern void
585 comp_frame_error(void) /* initialize frame error values */
586 {
587 uby8 *edone = NULL;
588 COLOR objamb;
589 double eest;
590 int neigh[NSAMPOK];
591 int nc;
592 int x, y, i;
593 register int n;
594
595 if (!silent) {
596 printf("\tComputing error map\n");
597 fflush(stdout);
598 }
599 if (acctab[0] <= FTINY) /* initialize accuracy table */
600 for (i = 256; i--; )
601 acctab[i] = errorf(i);
602 /* estimate sample error */
603 if (!curparams->ambounce && hirendparams.ambounce) {
604 /*
605 * Our error estimate for the initial value is based
606 * on the assumption that most of it comes from the
607 * lack of an interreflection calculation. The relative
608 * error should be less than the ambient value divided
609 * by the returned ray value -- we take half of this.
610 */
611 edone = (uby8 *)calloc(hres*vres, sizeof(uby8));
612 for (y = vres; y--; )
613 for (x = hres; x--; ) {
614 n = fndx(x, y);
615 if ((abuffer[n] != ALOWQ) | (obuffer[n] == OVOID))
616 continue;
617 if (!getambcolor(objamb, obuffer[n]))
618 continue;
619 multcolor(objamb, ambval);
620 if ((eest = bright(cbuffer[n])) <= FTINY)
621 continue;
622 eest = bright(objamb) / eest;
623 if (eest > 1.) /* should we report this? */
624 continue;
625 eest *= 0.50; /* use 50% ambient error */
626 i = errori(eest);
627 if (i < AMIN) i = AMIN;
628 else if (i >= ADISTANT/2) i = ADISTANT/2-1;
629 abuffer[n] = i;
630 edone[n] = 1;
631 }
632 }
633 /* final statistical estimate */
634 for (y = vres; y--; )
635 for (x = hres; x--; ) {
636 n = fndx(x, y);
637 if (abuffer[n] == ADISTANT)
638 continue; /* don't update these */
639 if (edone != NULL && edone[n])
640 continue; /* already done this */
641 if (sbuffer[n] >= 255) {
642 abuffer[n] = ADISTANT;
643 continue; /* can't take any more */
644 }
645 nc = getclosest(neigh, NSAMPOK, x, y);
646 if (nc <= 0) {
647 abuffer[n] = ANOVAL;
648 continue; /* no clue what to do for him */
649 }
650 i = errori(comperr(neigh, nc, sbuffer[n]));
651 if (i < AMIN) i = AMIN;
652 else if (i >= ADISTANT) i = ADISTANT-1;
653 abuffer[n] = i;
654 /* can't be better than closest */
655 if (i < abuffer[neigh[0]] && abuffer[neigh[0]] >= AMIN)
656 abuffer[n] = abuffer[neigh[0]];
657 }
658 if (edone != NULL)
659 free((void *)edone);
660 }
661
662
663 extern void
664 init_frame(void) /* render base (low quality) frame */
665 {
666 int restart;
667 /* allocate/swap buffers */
668 next_frame();
669 /* check rendering status */
670 restart = (!nobjects || vdef(MOVE));
671 if (!restart && curparams != &lorendparams && nprocs > 1)
672 restart = -1;
673 /* post low quality parameters */
674 if (curparams != &lorendparams)
675 ray_restore(curparams = &lorendparams);
676 if (restart > 0) { /* load new octree */
677 char *oct = getoctspec(fcur);
678 if (oct == NULL) {
679 sprintf(errmsg, "cannot get scene for frame %d", fcur);
680 error(USER, errmsg);
681 }
682 if (!silent) {
683 printf("\tLoading octree...");
684 fflush(stdout);
685 }
686 if (nprocs > 1)
687 ray_pinit(oct, nprocs);
688 else
689 ray_init(oct);
690 } else if (restart < 0) { /* update children */
691 if (!silent) {
692 printf("\tRestarting %d processes...", nprocs);
693 fflush(stdout);
694 }
695 ray_pclose(0);
696 ray_popen(nprocs);
697 }
698 if (restart && !silent)
699 printf("done\n");
700 /* sample frame buffer */
701 init_frame_sample();
702 /* initialize frame error */
703 comp_frame_error();
704 #if 0
705 {
706 float *ebuf = (float *)malloc(sizeof(float)*hres*vres);
707 char fnm[256];
708 register int n;
709 for (n = hres*vres; n--; )
710 ebuf[n] = acctab[abuffer[n]];
711 sprintf(fnm, vval(BASENAME), fcur);
712 strcat(fnm, "_inerr.pic");
713 write_map(ebuf, fnm);
714 free((void *)ebuf);
715 }
716 #endif
717 }
718
719
720 extern void
721 filter_frame(void) /* interpolation, motion-blur, and exposure */
722 {
723 double expval = expspec_val(getexp(fcur));
724 int x, y;
725 int neigh[NPINTERP];
726 int nc;
727 COLOR cval;
728 double w, wsum;
729 register int n;
730
731 #if 0
732 /* XXX TEMPORARY!! */
733 conspicuity();
734 write_map(cerrmap, "outcmap.pic");
735 {
736 float *ebuf = (float *)malloc(sizeof(float)*hres*vres);
737 for (n = hres*vres; n--; )
738 ebuf[n] = acctab[abuffer[n]];
739 write_map(ebuf, "outerr.pic");
740 free((void *)ebuf);
741 }
742 #endif
743
744 if (!silent) {
745 printf("\tFiltering frame\n");
746 fflush(stdout);
747 }
748 /* normalize samples */
749 for (y = vres; y--; )
750 for (x = hres; x--; ) {
751 n = fndx(x, y);
752 if (sbuffer[n] <= 1)
753 continue;
754 w = 1.0/(double)sbuffer[n];
755 scalecolor(cbuffer[n], w);
756 }
757 /* interpolate samples */
758 for (y = vres; y--; )
759 for (x = hres; x--; ) {
760 n = fndx(x, y);
761 if (sbuffer[n])
762 continue;
763 nc = getclosest(neigh, NPINTERP, x, y);
764 setcolor(cbuffer[n], 0., 0., 0.);
765 if (nc <= 0) { /* no acceptable neighbors */
766 if (y < vres-1)
767 nc = fndx(x, y+1);
768 else if (x < hres-1)
769 nc = fndx(x+1, y);
770 else
771 continue;
772 copycolor(cbuffer[n], cbuffer[nc]);
773 continue;
774 }
775 wsum = 0.;
776 while (nc-- > 0) {
777 copycolor(cval, cbuffer[neigh[nc]]);
778 w = sample_wt((neigh[nc]%hres) - x,
779 (neigh[nc]/hres) - y);
780 scalecolor(cval, w);
781 addcolor(cbuffer[n], cval);
782 wsum += w;
783 }
784 w = 1.0/wsum;
785 scalecolor(cbuffer[n], w);
786 }
787 /* motion blur if requested */
788 if (mblur > .02) {
789 int xs, ys, xl, yl;
790 int rise, run;
791 long rise2, run2;
792 int n2;
793 int cnt;
794 /* sum in motion streaks */
795 memset(outbuffer, '\0', sizeof(COLOR)*hres*vres);
796 memset(wbuffer, '\0', sizeof(float)*hres*vres);
797 for (y = vres; y--; )
798 for (x = hres; x--; ) {
799 n = fndx(x, y);
800 if (xmbuffer[n] == MO_UNK) {
801 run = rise = 0;
802 } else {
803 run = (int)(mblur*xmbuffer[n]);
804 rise = (int)(mblur*ymbuffer[n]);
805 }
806 if (!(run | rise)) {
807 addcolor(outbuffer[n], cbuffer[n]);
808 wbuffer[n] += 1.;
809 continue;
810 }
811 xl = x - run/4;
812 yl = y - rise/4;
813 if (run < 0) { xs = -1; run = -run; }
814 else xs = 1;
815 if (rise < 0) { ys = -1; rise = -rise; }
816 else ys = 1;
817 rise2 = run2 = 0L;
818 if (rise > run) {
819 cnt = rise + 1;
820 w = 1./cnt;
821 copycolor(cval, cbuffer[n]);
822 scalecolor(cval, w);
823 while (cnt)
824 if (rise2 >= run2) {
825 if ((xl >= 0) & (xl < hres) &
826 (yl >= 0) & (yl < vres)) {
827 n2 = fndx(xl, yl);
828 addcolor(outbuffer[n2],
829 cval);
830 wbuffer[n2] += w;
831 }
832 yl += ys;
833 run2 += run;
834 cnt--;
835 } else {
836 xl += xs;
837 rise2 += rise;
838 }
839 } else {
840 cnt = run + 1;
841 w = 1./cnt;
842 copycolor(cval, cbuffer[n]);
843 scalecolor(cval, w);
844 while (cnt)
845 if (run2 >= rise2) {
846 if ((xl >= 0) & (xl < hres) &
847 (yl >= 0) & (yl < vres)) {
848 n2 = fndx(xl, yl);
849 addcolor(outbuffer[n2],
850 cval);
851 wbuffer[n2] += w;
852 }
853 xl += xs;
854 rise2 += rise;
855 cnt--;
856 } else {
857 yl += ys;
858 run2 += run;
859 }
860 }
861 }
862 /* compute final results */
863 for (y = vres; y--; )
864 for (x = hres; x--; ) {
865 n = fndx(x, y);
866 if (wbuffer[n] <= FTINY)
867 continue;
868 w = 1./wbuffer[n];
869 scalecolor(outbuffer[n], w);
870 }
871 } else
872 for (n = hres*vres; n--; )
873 copycolor(outbuffer[n], cbuffer[n]);
874 /*
875 for (n = hres*vres; n--; )
876 if (!sbuffer[n])
877 setcolor(outbuffer[n], 0., 0., 0.);
878 */
879 /* adjust exposure */
880 if ((expval < 0.99) | (expval > 1.01))
881 for (n = hres*vres; n--; )
882 scalecolor(outbuffer[n], expval);
883 #if 0
884 {
885 float *sbuf = (float *)malloc(sizeof(float)*hres*vres);
886 char fnm[256];
887 sprintf(fnm, vval(BASENAME), fcur);
888 strcat(fnm, "_outsamp.pic");
889 for (n = hres*vres; n--; )
890 sbuf[n] = (float)sbuffer[n];
891 write_map(sbuf, fnm);
892 free((void *)sbuf);
893 }
894 #endif
895 }
896
897
898 extern void
899 send_frame(void) /* send frame to destination */
900 {
901 char pfname[1024];
902 double d;
903 FILE *fp;
904 int y;
905 /* open output picture */
906 sprintf(pfname, vval(BASENAME), fcur);
907 strcat(pfname, ".hdr");
908 fp = fopen(pfname, "w");
909 if (fp == NULL) {
910 sprintf(errmsg, "cannot open output frame \"%s\"", pfname);
911 error(SYSTEM, errmsg);
912 }
913 SET_FILE_BINARY(fp);
914 if (!silent) {
915 printf("\tWriting to \"%s\"\n", pfname);
916 fflush(stdout);
917 }
918 /* write header */
919 newheader("RADIANCE", fp);
920 printargs(gargc, gargv, fp);
921 fprintf(fp, "SOFTWARE= %s\n", VersionID);
922 fprintf(fp, "FRAME=%d\n", fcur);
923 fputnow(fp);
924 fputs(VIEWSTR, fp); fprintview(&vw, fp); fputc('\n', fp);
925 d = expspec_val(getexp(fcur));
926 if ((d < 0.99) | (d > 1.01))
927 fputexpos(d, fp);
928 d = viewaspect(&vw) * hres / vres;
929 if ((d < 0.99) | (d > 1.01))
930 fputaspect(d, fp);
931 fputformat(COLRFMT, fp);
932 fputc('\n', fp); /* end header */
933 fprtresolu(hres, vres, fp);
934 if (fflush(fp) == EOF)
935 goto writerr;
936 #if (PIXSTANDARD != (YMAJOR|YDECR))
937 error(CONSISTENCY, "bad code in send_frame");
938 #endif
939 for (y = vres; y--; ) /* write scanlines */
940 if (fwritescan(outbuffer+y*hres, hres, fp) < 0)
941 goto writerr;
942 if (fclose(fp) == EOF)
943 goto writerr;
944 return; /* all is well */
945 writerr:
946 sprintf(errmsg, "error writing frame \"%s\"", pfname);
947 error(SYSTEM, errmsg);
948 }
949
950
951 extern void
952 free_frame(void) /* free frame allocation */
953 {
954 if (cbuffer == NULL)
955 return;
956 free((void *)cbuffer); cbuffer = NULL;
957 free((void *)zbuffer); zbuffer = NULL;
958 free((void *)obuffer); obuffer = NULL;
959 free((void *)xmbuffer); xmbuffer = NULL;
960 free((void *)ymbuffer); ymbuffer = NULL;
961 free((void *)cprev); cprev = NULL;
962 free((void *)zprev); zprev = NULL;
963 free((void *)oprev); oprev = NULL;
964 cerrmap = NULL;
965 val2map = NULL;
966 hres = vres = 0;
967 vw.type = vwprev.type = 0;
968 frm_stop = 0;
969 }