ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rv2.c
Revision: 2.57
Committed: Fri Aug 29 00:30:51 2008 UTC (15 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.56: +17 -4 lines
Log Message:
Improved determination of when we actually have a new parameter

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rv2.c,v 2.56 2008/08/21 16:13:00 greg Exp $";
3 #endif
4 /*
5 * rv2.c - command routines used in tracing a view.
6 *
7 * External symbols declared in rpaint.h
8 */
9
10 #include "copyright.h"
11
12 #include <ctype.h>
13 #include <string.h>
14
15 #include "platform.h"
16 #include "ray.h"
17 #include "source.h"
18 #include "ambient.h"
19 #include "otypes.h"
20 #include "rpaint.h"
21
22 extern int psample; /* pixel sample size */
23 extern double maxdiff; /* max. sample difference */
24
25 #define CTRL(c) ((c)-'@')
26
27 #ifdef SMLFLT
28 #define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3)
29 #else
30 #define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3)
31 #endif
32
33 extern char rifname[128]; /* rad input file name */
34
35 extern char *progname;
36 extern char *octname;
37
38
39 void
40 getframe( /* get a new frame */
41 char *s
42 )
43 {
44 if (getrect(s, &pframe) < 0)
45 return;
46 pdepth = 0;
47 }
48
49
50 void
51 getrepaint( /* get area and repaint */
52 char *s
53 )
54 {
55 RECT box;
56
57 if (getrect(s, &box) < 0)
58 return;
59 paintrect(&ptrunk, &box);
60 }
61
62
63 void
64 getview( /* get/show view parameters */
65 char *s
66 )
67 {
68 FILE *fp;
69 char buf[128];
70 char *fname;
71 int change = 0;
72 VIEW nv = ourview;
73
74 while (isspace(*s))
75 s++;
76 if (*s == '-') { /* command line parameters */
77 if (sscanview(&nv, s))
78 newview(&nv);
79 else
80 error(COMMAND, "bad view option(s)");
81 return;
82 }
83 if (sscanf(s, "%s", buf) == 1) { /* write parameters to a file */
84 if ((fname = getpath(buf, NULL, 0)) == NULL ||
85 (fp = fopen(fname, "a")) == NULL) {
86 sprintf(errmsg, "cannot open \"%s\"", buf);
87 error(COMMAND, errmsg);
88 return;
89 }
90 fputs(progname, fp);
91 fprintview(&ourview, fp);
92 fputs(sskip(s), fp);
93 putc('\n', fp);
94 fclose(fp);
95 return;
96 }
97 sprintf(buf, "view type (%c): ", ourview.type);
98 (*dev->comout)(buf);
99 (*dev->comin)(buf, NULL);
100 if (buf[0] == CTRL('C')) return;
101 if (buf[0] && buf[0] != ourview.type) {
102 nv.type = buf[0];
103 change++;
104 }
105 sprintf(buf, "view point (%.6g %.6g %.6g): ",
106 ourview.vp[0], ourview.vp[1], ourview.vp[2]);
107 (*dev->comout)(buf);
108 (*dev->comin)(buf, NULL);
109 if (buf[0] == CTRL('C')) return;
110 if (sscanvec(buf, nv.vp))
111 change++;
112 sprintf(buf, "view direction (%.6g %.6g %.6g): ",
113 ourview.vdir[0]*ourview.vdist,
114 ourview.vdir[1]*ourview.vdist,
115 ourview.vdir[2]*ourview.vdist);
116 (*dev->comout)(buf);
117 (*dev->comin)(buf, NULL);
118 if (buf[0] == CTRL('C')) return;
119 if (sscanvec(buf, nv.vdir)) {
120 nv.vdist = 1.;
121 change++;
122 }
123 sprintf(buf, "view up (%.6g %.6g %.6g): ",
124 ourview.vup[0], ourview.vup[1], ourview.vup[2]);
125 (*dev->comout)(buf);
126 (*dev->comin)(buf, NULL);
127 if (buf[0] == CTRL('C')) return;
128 if (sscanvec(buf, nv.vup))
129 change++;
130 sprintf(buf, "view horiz and vert size (%.6g %.6g): ",
131 ourview.horiz, ourview.vert);
132 (*dev->comout)(buf);
133 (*dev->comin)(buf, NULL);
134 if (buf[0] == CTRL('C')) return;
135 if (sscanf(buf, "%lf %lf", &nv.horiz, &nv.vert) == 2)
136 change++;
137 sprintf(buf, "fore and aft clipping plane (%.6g %.6g): ",
138 ourview.vfore, ourview.vaft);
139 (*dev->comout)(buf);
140 (*dev->comin)(buf, NULL);
141 if (buf[0] == CTRL('C')) return;
142 if (sscanf(buf, "%lf %lf", &nv.vfore, &nv.vaft) == 2)
143 change++;
144 sprintf(buf, "view shift and lift (%.6g %.6g): ",
145 ourview.hoff, ourview.voff);
146 (*dev->comout)(buf);
147 (*dev->comin)(buf, NULL);
148 if (buf[0] == CTRL('C')) return;
149 if (sscanf(buf, "%lf %lf", &nv.hoff, &nv.voff) == 2)
150 change++;
151 if (change)
152 newview(&nv);
153 }
154
155
156 void
157 lastview( /* return to a previous view */
158 char *s
159 )
160 {
161 char buf[128];
162 char *fname;
163 int success;
164 VIEW nv;
165
166 if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */
167 nv = stdview;
168 if ((fname = getpath(buf, "", R_OK)) == NULL ||
169 (success = viewfile(fname, &nv, NULL)) == -1) {
170 sprintf(errmsg, "cannot open \"%s\"", buf);
171 error(COMMAND, errmsg);
172 return;
173 }
174 if (!success)
175 error(COMMAND, "wrong file format");
176 else
177 newview(&nv);
178 return;
179 }
180 if (oldview.type == 0) { /* no old view! */
181 error(COMMAND, "no previous view");
182 return;
183 }
184 nv = ourview;
185 ourview = oldview;
186 oldview = nv;
187 newimage(NULL);
188 }
189
190
191 void
192 saveview( /* save view to rad file */
193 char *s
194 )
195 {
196 char view[64];
197 char *fname;
198 FILE *fp;
199
200 if (*atos(view, sizeof(view), s)) {
201 if (isint(view)) {
202 error(COMMAND, "cannot write view by number");
203 return;
204 }
205 s = sskip(s);
206 }
207 while (isspace(*s))
208 s++;
209 if (*s)
210 atos(rifname, sizeof(rifname), s);
211 else if (rifname[0] == '\0') {
212 error(COMMAND, "no previous rad file");
213 return;
214 }
215 if ((fname = getpath(rifname, NULL, 0)) == NULL ||
216 (fp = fopen(fname, "a")) == NULL) {
217 sprintf(errmsg, "cannot open \"%s\"", rifname);
218 error(COMMAND, errmsg);
219 return;
220 }
221 fputs("view= ", fp);
222 fputs(view, fp);
223 fprintview(&ourview, fp);
224 putc('\n', fp);
225 fclose(fp);
226 }
227
228
229 void
230 loadview( /* load view from rad file */
231 char *s
232 )
233 {
234 char buf[512];
235 char *fname;
236 FILE *fp;
237 VIEW nv;
238
239 strcpy(buf, "rad -n -s -V -v ");
240 if (sscanf(s, "%s", buf+strlen(buf)) == 1)
241 s = sskip(s);
242 else
243 strcat(buf, "1");
244 if (*s)
245 atos(rifname, sizeof(rifname), s);
246 else if (rifname[0] == '\0') {
247 error(COMMAND, "no previous rad file");
248 return;
249 }
250 if ((fname = getpath(rifname, "", R_OK)) == NULL) {
251 sprintf(errmsg, "cannot access \"%s\"", rifname);
252 error(COMMAND, errmsg);
253 return;
254 }
255 sprintf(buf+strlen(buf), " %s", fname);
256 if ((fp = popen(buf, "r")) == NULL) {
257 error(COMMAND, "cannot run rad");
258 return;
259 }
260 buf[0] = '\0';
261 fgets(buf, sizeof(buf), fp);
262 pclose(fp);
263 nv = stdview;
264 if (!sscanview(&nv, buf)) {
265 error(COMMAND, "rad error -- no such view?");
266 return;
267 }
268 newview(&nv);
269 }
270
271
272 void
273 getaim( /* aim camera */
274 char *s
275 )
276 {
277 VIEW nv = ourview;
278 double zfact;
279
280 if (getinterest(s, 1, nv.vdir, &zfact) < 0)
281 return;
282 zoomview(&nv, zfact);
283 newview(&nv);
284 }
285
286
287 void
288 getfocus( /* set focus distance */
289 char *s
290 )
291 {
292 char buf[64];
293 double dist;
294
295 if (sscanf(s, "%lf", &dist) < 1) {
296 int x, y;
297 RAY thisray;
298 if (dev->getcur == NULL)
299 return;
300 (*dev->comout)("Pick focus point\n");
301 if ((*dev->getcur)(&x, &y) == ABORT)
302 return;
303 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
304 &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
305 error(COMMAND, "not on image");
306 return;
307 }
308 rayorigin(&thisray, PRIMARY, NULL, NULL);
309 if (!localhit(&thisray, &thescene)) {
310 error(COMMAND, "not a local object");
311 return;
312 }
313 dist = thisray.rot;
314 } else if (dist <= .0) {
315 error(COMMAND, "focus distance must be positive");
316 return;
317 }
318 ourview.vdist = dist;
319 sprintf(buf, "Focus distance set to %f\n", dist);
320 (*dev->comout)(buf);
321 }
322
323
324 void
325 getmove( /* move camera */
326 char *s
327 )
328 {
329 FVECT vc;
330 double mag;
331
332 if (getinterest(s, 0, vc, &mag) < 0)
333 return;
334 moveview(0.0, 0.0, mag, vc);
335 }
336
337
338 void
339 getrotate( /* rotate camera */
340 char *s
341 )
342 {
343 VIEW nv = ourview;
344 FVECT v1;
345 double angle, elev, zfact;
346
347 elev = 0.0; zfact = 1.0;
348 if (sscanf(s, "%lf %lf %lf", &angle, &elev, &zfact) < 1) {
349 error(COMMAND, "missing angle");
350 return;
351 }
352 spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.));
353 if (elev != 0.0) {
354 fcross(v1, nv.vdir, ourview.vup);
355 normalize(v1);
356 spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.));
357 }
358 zoomview(&nv, zfact);
359 newview(&nv);
360 }
361
362
363 void
364 getpivot( /* pivot viewpoint */
365 char *s
366 )
367 {
368 FVECT vc;
369 double angle, elev, mag;
370
371 elev = 0.0;
372 if (sscanf(s, "%lf %lf", &angle, &elev) < 1) {
373 error(COMMAND, "missing angle");
374 return;
375 }
376 if (getinterest(sskip2(s,2), 0, vc, &mag) < 0)
377 return;
378 moveview(angle, elev, mag, vc);
379 }
380
381
382 void
383 getexposure( /* get new exposure */
384 char *s
385 )
386 {
387 char buf[128];
388 char *cp;
389 int x, y;
390 PNODE *p = &ptrunk;
391 int adapt = 0;
392 double e = 1.0;
393
394 for (cp = s; isspace(*cp); cp++)
395 ;
396 if (*cp == '@') {
397 adapt++;
398 while (isspace(*++cp))
399 ;
400 }
401 if (*cp == '\0') { /* normalize to point */
402 if (dev->getcur == NULL)
403 return;
404 (*dev->comout)("Pick point for exposure\n");
405 if ((*dev->getcur)(&x, &y) == ABORT)
406 return;
407 p = findrect(x, y, &ptrunk, -1);
408 } else {
409 if (*cp == '=') { /* absolute setting */
410 p = NULL;
411 e = 1.0/exposure;
412 for (cp++; isspace(*cp); cp++)
413 ;
414 if (*cp == '\0') { /* interactive */
415 sprintf(buf, "exposure (%f): ", exposure);
416 (*dev->comout)(buf);
417 (*dev->comin)(buf, NULL);
418 for (cp = buf; isspace(*cp); cp++)
419 ;
420 if (*cp == '\0')
421 return;
422 }
423 }
424 if (*cp == '+' || *cp == '-') /* f-stops */
425 e *= pow(2.0, atof(cp));
426 else /* multiplier */
427 e *= atof(cp);
428 }
429 if (p != NULL) { /* relative setting */
430 if (bright(p->v) < 1e-15) {
431 error(COMMAND, "cannot normalize to zero");
432 return;
433 }
434 if (adapt)
435 e *= 106./pow(1.219+pow(luminance(p->v)/exposure,.4),2.5)/exposure;
436 else
437 e *= 0.5 / bright(p->v);
438 }
439 if (e <= FTINY || fabs(1.0 - e) <= FTINY)
440 return;
441 scalepict(&ptrunk, e);
442 exposure *= e;
443 redraw();
444 }
445
446 typedef union {int i; double d; COLOR C;} *MyUptr;
447 #define FEQ(x,y) (fabs((x)-(y)) <= FTINY)
448
449 int
450 getparam( /* get variable from user */
451 char *str,
452 char *dsc,
453 int typ,
454 void *p
455 )
456 {
457 MyUptr ptr = (MyUptr)p;
458 int i0;
459 double d0, d1, d2;
460 char buf[48];
461
462 switch (typ) {
463 case 'i': /* integer */
464 if (sscanf(str, "%d", &i0) != 1) {
465 (*dev->comout)(dsc);
466 sprintf(buf, " (%d): ", ptr->i);
467 (*dev->comout)(buf);
468 (*dev->comin)(buf, NULL);
469 if (sscanf(buf, "%d", &i0) != 1)
470 return(0);
471 }
472 if (ptr->i == i0)
473 return(0);
474 ptr->i = i0;
475 break;
476 case 'r': /* real */
477 if (sscanf(str, "%lf", &d0) != 1) {
478 (*dev->comout)(dsc);
479 sprintf(buf, " (%.6g): ", ptr->d);
480 (*dev->comout)(buf);
481 (*dev->comin)(buf, NULL);
482 if (sscanf(buf, "%lf", &d0) != 1)
483 return(0);
484 }
485 if (FEQ(ptr->d, d0))
486 return(0);
487 ptr->d = d0;
488 break;
489 case 'b': /* boolean */
490 if (sscanf(str, "%1s", buf) != 1) {
491 (*dev->comout)(dsc);
492 sprintf(buf, "? (%c): ", ptr->i ? 'y' : 'n');
493 (*dev->comout)(buf);
494 (*dev->comin)(buf, NULL);
495 if (buf[0] == '\0')
496 return(0);
497 }
498 if (strchr("yY+1tTnN-0fF", buf[0]) == NULL)
499 return(0);
500 i0 = strchr("yY+1tT", buf[0]) != NULL;
501 if (ptr->i == i0)
502 return(0);
503 ptr->i = i0;
504 break;
505 case 'C': /* color */
506 if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) {
507 (*dev->comout)(dsc);
508 sprintf(buf, " (%.6g %.6g %.6g): ",
509 colval(ptr->C,RED),
510 colval(ptr->C,GRN),
511 colval(ptr->C,BLU));
512 (*dev->comout)(buf);
513 (*dev->comin)(buf, NULL);
514 if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3)
515 return(0);
516 }
517 if (FEQ(colval(ptr->C,RED), d0) &&
518 FEQ(colval(ptr->C,GRN), d1) &&
519 FEQ(colval(ptr->C,BLU), d2))
520 return(0);
521 setcolor(ptr->C, d0, d1, d2);
522 break;
523 default:
524 return(0); /* shouldn't happen */
525 }
526 newparam++;
527 return(1);
528 }
529
530
531 void
532 setparam( /* get/set program parameter */
533 char *s
534 )
535 {
536 char buf[128];
537
538 if (s[0] == '\0') {
539 (*dev->comout)(
540 "aa ab ad ar as av aw b bv dc dv dj ds dt i lr lw me ma mg ms ps pt sj st u: ");
541 (*dev->comin)(buf, NULL);
542 s = buf;
543 }
544 switch (s[0]) {
545 case 'u': /* uncorrelated sampling */
546 getparam(s+1, "uncorrelated sampling", 'b',
547 (void *)&rand_samp);
548 break;
549 case 'l': /* limit */
550 switch (s[1]) {
551 case 'w': /* weight */
552 getparam(s+2, "limit weight", 'r',
553 (void *)&minweight);
554 break;
555 case 'r': /* reflection */
556 getparam(s+2, "limit reflection", 'i',
557 (void *)&maxdepth);
558 break;
559 default:
560 goto badparam;
561 }
562 break;
563 case 'd': /* direct */
564 switch (s[1]) {
565 case 'j': /* jitter */
566 getparam(s+2, "direct jitter", 'r',
567 (void *)&dstrsrc);
568 break;
569 case 'c': /* certainty */
570 getparam(s+2, "direct certainty", 'r',
571 (void *)&shadcert);
572 break;
573 case 't': /* threshold */
574 getparam(s+2, "direct threshold", 'r',
575 (void *)&shadthresh);
576 break;
577 case 'v': /* visibility */
578 getparam(s+2, "direct visibility", 'b',
579 (void *)&directvis);
580 break;
581 case 's': /* sampling */
582 getparam(s+2, "direct sampling", 'r',
583 (void *)&srcsizerat);
584 break;
585 default:
586 goto badparam;
587 }
588 break;
589 case 'b': /* back faces or black and white */
590 switch (s[1]) {
591 case 'v': /* back face visibility */
592 getparam(s+2, "back face visibility", 'b',
593 (void *)&backvis);
594 break;
595 case '\0': /* black and white */
596 case ' ':
597 case 'y': case 'Y': case 't': case 'T': case '1': case '+':
598 case 'n': case 'N': case 'f': case 'F': case '0': case '-':
599 getparam(s+1, "black and white", 'b',
600 (void *)&greyscale);
601 break;
602 default:
603 goto badparam;
604 }
605 break;
606 case 'i': /* irradiance */
607 getparam(s+1, "irradiance", 'b',
608 (void *)&do_irrad);
609 break;
610 case 'a': /* ambient */
611 switch (s[1]) {
612 case 'v': /* value */
613 getparam(s+2, "ambient value", 'C',
614 (void *)ambval);
615 break;
616 case 'w': /* weight */
617 getparam(s+2, "ambient value weight", 'i',
618 (void *)&ambvwt);
619 break;
620 case 'a': /* accuracy */
621 if (getparam(s+2, "ambient accuracy", 'r',
622 (void *)&ambacc))
623 setambacc(ambacc);
624 break;
625 case 'd': /* divisions */
626 getparam(s+2, "ambient divisions", 'i',
627 (void *)&ambdiv);
628 break;
629 case 's': /* samples */
630 getparam(s+2, "ambient super-samples", 'i',
631 (void *)&ambssamp);
632 break;
633 case 'b': /* bounces */
634 getparam(s+2, "ambient bounces", 'i',
635 (void *)&ambounce);
636 break;
637 case 'r':
638 if (getparam(s+2, "ambient resolution", 'i',
639 (void *)&ambres))
640 setambres(ambres);
641 break;
642 default:
643 goto badparam;
644 }
645 break;
646 case 'm': /* medium */
647 switch (s[1]) {
648 case 'e': /* extinction coefficient */
649 getparam(s+2, "extinction coefficient", 'C',
650 (void *)cextinction);
651 break;
652 case 'a': /* scattering albedo */
653 getparam(s+2, "scattering albedo", 'C',
654 (void *)salbedo);
655 break;
656 case 'g': /* scattering eccentricity */
657 getparam(s+2, "scattering eccentricity", 'r',
658 (void *)&seccg);
659 break;
660 case 's': /* sampling distance */
661 getparam(s+2, "mist sampling distance", 'r',
662 (void *)&ssampdist);
663 break;
664 default:
665 goto badparam;
666 }
667 break;
668 case 'p': /* pixel */
669 switch (s[1]) {
670 case 's': /* sample */
671 if (getparam(s+2, "pixel sample", 'i',
672 (void *)&psample))
673 pdepth = 0;
674 break;
675 case 't': /* threshold */
676 if (getparam(s+2, "pixel threshold", 'r',
677 (void *)&maxdiff))
678 pdepth = 0;
679 break;
680 default:
681 goto badparam;
682 }
683 break;
684 case 's': /* specular */
685 switch (s[1]) {
686 case 'j': /* jitter */
687 getparam(s+2, "specular jitter", 'r',
688 (void *)&specjitter);
689 break;
690 case 't': /* threshold */
691 getparam(s+2, "specular threshold", 'r',
692 (void *)&specthresh);
693 break;
694 default:
695 goto badparam;
696 }
697 break;
698 case '\0': /* nothing */
699 break;
700 default:;
701 badparam:
702 *sskip(s) = '\0';
703 sprintf(errmsg, "%s: unknown variable", s);
704 error(COMMAND, errmsg);
705 break;
706 }
707 }
708
709
710 void
711 traceray( /* trace a single ray */
712 char *s
713 )
714 {
715 char buf[128];
716 int x, y;
717 OBJREC *ino;
718 RAY thisray;
719
720 thisray.rmax = 0.0;
721
722 if (!sscanvec(s, thisray.rorg) ||
723 !sscanvec(sskip2(s,3), thisray.rdir)) {
724
725 if (dev->getcur == NULL)
726 return;
727 (*dev->comout)("Pick ray\n");
728 if ((*dev->getcur)(&x, &y) == ABORT)
729 return;
730
731 if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
732 &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) {
733 error(COMMAND, "not on image");
734 return;
735 }
736
737 } else if (normalize(thisray.rdir) == 0.0) {
738 error(COMMAND, "zero ray direction");
739 return;
740 }
741
742 ray_trace(&thisray);
743
744 if (thisray.ro == NULL)
745 (*dev->comout)("ray hit nothing");
746 else {
747 OBJREC *mat = NULL;
748 OBJREC *mod = NULL;
749 char matspec[256];
750 matspec[0] = '\0';
751 if (thisray.ro->omod != OVOID) {
752 mod = objptr(thisray.ro->omod);
753 mat = findmaterial(mod);
754 }
755 if (thisray.rod < 0.0)
756 strcpy(matspec, "back of ");
757 if (mod != NULL) {
758 strcat(matspec, mod->oname);
759 if (mat != mod && mat != NULL)
760 sprintf(matspec+strlen(matspec),
761 " (%s)", mat->oname);
762 } else
763 strcat(matspec, VOIDID);
764 sprintf(buf, "ray hit %s %s \"%s\"", matspec,
765 ofun[thisray.ro->otype].funame,
766 thisray.ro->oname);
767 if ((ino = objptr(thisray.robj)) != thisray.ro)
768 sprintf(buf+strlen(buf), " in %s \"%s\"",
769 ofun[ino->otype].funame, ino->oname);
770 (*dev->comout)(buf);
771 (*dev->comin)(buf, NULL);
772 if (thisray.rot >= FHUGE)
773 (*dev->comout)("at infinity");
774 else {
775 sprintf(buf, "at (%.6g %.6g %.6g) (%.6g)",
776 thisray.rop[0], thisray.rop[1],
777 thisray.rop[2], thisray.rt);
778 (*dev->comout)(buf);
779 }
780 (*dev->comin)(buf, NULL);
781 sprintf(buf, "value (%.5g %.5g %.5g) (%.3gL)",
782 colval(thisray.rcol,RED),
783 colval(thisray.rcol,GRN),
784 colval(thisray.rcol,BLU),
785 luminance(thisray.rcol));
786 (*dev->comout)(buf);
787 }
788 (*dev->comin)(buf, NULL);
789 }
790
791
792 void
793 writepict( /* write the picture to a file */
794 char *s
795 )
796 {
797 static char buf[128];
798 char *fname;
799 FILE *fp;
800 COLR *scanline;
801 int y;
802
803 while (isspace(*s))
804 s++;
805 if (*s)
806 atos(buf, sizeof(buf), s);
807 else if (buf[0] == '\0') {
808 error(COMMAND, "no file");
809 return;
810 }
811 if ((fname = getpath(buf, NULL, 0)) == NULL ||
812 (fp = fopen(fname, "w")) == NULL) {
813 sprintf(errmsg, "cannot open \"%s\"", buf);
814 error(COMMAND, errmsg);
815 return;
816 }
817 SET_FILE_BINARY(fp);
818 (*dev->comout)("writing \"");
819 (*dev->comout)(fname);
820 (*dev->comout)("\"...\n");
821 /* write header */
822 newheader("RADIANCE", fp);
823 fputs(progname, fp);
824 fprintview(&ourview, fp);
825 if (octname != NULL)
826 fprintf(fp, " %s\n", octname);
827 else
828 putc('\n', fp);
829 fprintf(fp, "SOFTWARE= %s\n", VersionID);
830 fputnow(fp);
831 if (exposure != 1.0)
832 fputexpos(exposure, fp);
833 if (dev->pixaspect != 1.0)
834 fputaspect(dev->pixaspect, fp);
835 fputformat(COLRFMT, fp);
836 putc('\n', fp);
837 fprtresolu(hresolu, vresolu, fp);
838
839 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
840 if (scanline == NULL) {
841 error(COMMAND, "not enough memory!");
842 fclose(fp);
843 unlink(fname);
844 return;
845 }
846 for (y = vresolu-1; y >= 0; y--) {
847 getpictcolrs(y, scanline, &ptrunk, hresolu, vresolu);
848 if (fwritecolrs(scanline, hresolu, fp) < 0)
849 break;
850 }
851 free((void *)scanline);
852 if (fclose(fp) < 0)
853 error(COMMAND, "write error");
854 }