ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/sceneio.c
Revision: 2.5
Committed: Thu Sep 22 02:15:56 2011 UTC (12 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R2P1, rad5R3
Changes since 2.4: +22 -17 lines
Log Message:
Minor optimizations

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: sceneio.c,v 2.4 2003/06/26 00:58:09 schorsch Exp $";
3 #endif
4 /*
5 * Portable, binary Radiance i/o routines.
6 *
7 * Called from octree and mesh i/o routines.
8 */
9
10 #include "copyright.h"
11 #include "standard.h"
12 #include "octree.h"
13 #include "object.h"
14 #include "otypes.h"
15
16 static OBJECT object0; /* zeroeth object */
17 static short otypmap[NUMOTYPE+32]; /* object type map */
18
19
20 static int
21 getobj( /* get next object */
22 FILE *fp,
23 int objsiz
24 )
25 {
26 char sbuf[MAXSTR];
27 int obj;
28 int i;
29 long m;
30 OBJREC *objp;
31
32 i = getint(1, fp);
33 if (i == -1)
34 return(OVOID); /* terminator */
35 if ((obj = newobject()) == OVOID)
36 error(SYSTEM, "out of object space");
37 objp = objptr(obj);
38 if ((objp->otype = otypmap[i]) < 0)
39 error(USER, "reference to unknown type");
40 if ((m = getint(objsiz, fp)) != OVOID) {
41 m += object0;
42 if ((OBJECT)m != m)
43 error(INTERNAL, "too many objects in getobj");
44 }
45 objp->omod = m;
46 objp->oname = savqstr(getstr(sbuf, fp));
47 if ((objp->oargs.nsargs = getint(2, fp)) > 0) {
48 objp->oargs.sarg = (char **)malloc
49 (objp->oargs.nsargs*sizeof(char *));
50 if (objp->oargs.sarg == NULL)
51 goto memerr;
52 for (i = 0; i < objp->oargs.nsargs; i++)
53 objp->oargs.sarg[i] = savestr(getstr(sbuf, fp));
54 } else
55 objp->oargs.sarg = NULL;
56 #ifdef IARGS
57 if ((objp->oargs.niargs = getint(2, fp)) > 0) {
58 objp->oargs.iarg = (long *)malloc
59 (objp->oargs.niargs*sizeof(long));
60 if (objp->oargs.iarg == NULL)
61 goto memerr;
62 for (i = 0; i < objp->oargs.niargs; i++)
63 objp->oargs.iarg[i] = getint(4, fp);
64 } else
65 objp->oargs.iarg = NULL;
66 #endif
67 if ((objp->oargs.nfargs = getint(2, fp)) > 0) {
68 objp->oargs.farg = (RREAL *)malloc
69 (objp->oargs.nfargs*sizeof(RREAL));
70 if (objp->oargs.farg == NULL)
71 goto memerr;
72 for (i = 0; i < objp->oargs.nfargs; i++)
73 objp->oargs.farg[i] = getflt(fp);
74 } else
75 objp->oargs.farg = NULL;
76 if (feof(fp))
77 error(SYSTEM, "unexpected EOF in getobj");
78 /* initialize */
79 objp->os = NULL;
80 /* insert */
81 insertobject(obj);
82 return(obj);
83 memerr:
84 error(SYSTEM, "out of memory in getobj");
85 return 0; /* pro forma return */
86 }
87
88
89 void
90 readscene( /* read binary scene description */
91 FILE *fp,
92 int objsiz
93 )
94 {
95 char sbuf[32];
96 int i;
97 /* record starting object */
98 object0 = nobjects;
99 /* read type map */
100 for (i = 0; getstr(sbuf, fp) != NULL && sbuf[0]; i++)
101 if ((otypmap[i] = otype(sbuf)) < 0) {
102 sprintf(errmsg, "unknown object type \"%s\"",
103 sbuf);
104 error(WARNING, errmsg);
105 }
106 /* read objects */
107 while (getobj(fp, objsiz) != OVOID)
108 ;
109 }
110
111
112 static void
113 putobj( /* write out object */
114 OBJREC *o,
115 FILE *fp
116 )
117 {
118 int i;
119
120 if (o == NULL) { /* terminator */
121 putint(-1L, 1, fp);
122 return;
123 }
124 putint((long)o->otype, 1, fp);
125 putint((long)o->omod, sizeof(OBJECT), fp);
126 putstr(o->oname, fp);
127 putint((long)o->oargs.nsargs, 2, fp);
128 for (i = 0; i < o->oargs.nsargs; i++)
129 putstr(o->oargs.sarg[i], fp);
130 #ifdef IARGS
131 putint((long)o->oargs.niargs, 2, fp);
132 for (i = 0; i < o->oargs.niargs; i++)
133 putint((long)o->oargs.iarg[i], 4, fp);
134 #endif
135 putint((long)o->oargs.nfargs, 2, fp);
136 for (i = 0; i < o->oargs.nfargs; i++)
137 putflt(o->oargs.farg[i], fp);
138 }
139
140
141 void
142 writescene( /* write binary scene description */
143 int firstobj,
144 int nobjs,
145 FILE *fp
146 )
147 {
148 int i;
149 /* write out type list */
150 for (i = 0; i < NUMOTYPE; i++)
151 putstr(ofun[i].funame, fp);
152 putstr("", fp);
153 /* write objects */
154 for (i = firstobj; i < firstobj+nobjs; i++)
155 putobj(objptr(i), fp);
156 putobj(NULL, fp); /* terminator */
157 if (ferror(fp))
158 error(SYSTEM, "write error in writescene");
159 }