Back to index

radiance  4R0+20100331
sceneio.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: sceneio.c,v 2.4 2003/06/26 00:58:09 schorsch Exp $";
00003 #endif
00004 /*
00005  *  Portable, binary Radiance i/o routines.
00006  *
00007  *  Called from octree and mesh i/o routines.
00008  */
00009 
00010 #include "copyright.h"
00011 #include "standard.h"
00012 #include "octree.h"
00013 #include "object.h"
00014 #include "otypes.h"
00015 
00016 static OBJECT  object0;                   /* zeroeth object */
00017 static short  otypmap[NUMOTYPE+32];       /* object type map */
00018 
00019 
00020 static int
00021 getobj(fp, objsiz)                 /* get next object */
00022 FILE   *fp;
00023 int    objsiz;
00024 {
00025        char  sbuf[MAXSTR];
00026        int  obj;
00027        register int  i;
00028        register long  m;
00029        register OBJREC       *objp;
00030 
00031        i = getint(1, fp);
00032        if (i == -1)
00033               return(OVOID);              /* terminator */
00034        if ((obj = newobject()) == OVOID)
00035               error(SYSTEM, "out of object space");
00036        objp = objptr(obj);
00037        if ((objp->otype = otypmap[i]) < 0)
00038               error(USER, "reference to unknown type");
00039        if ((m = getint(objsiz, fp)) != OVOID) {
00040               m += object0;
00041               if ((OBJECT)m != m)
00042                      error(INTERNAL, "too many objects in getobj");
00043        }
00044        objp->omod = m;
00045        objp->oname = savqstr(getstr(sbuf, fp));
00046        if ((objp->oargs.nsargs = getint(2, fp)) > 0) {
00047               objp->oargs.sarg = (char **)malloc
00048                             (objp->oargs.nsargs*sizeof(char *));
00049               if (objp->oargs.sarg == NULL)
00050                      goto memerr;
00051               for (i = 0; i < objp->oargs.nsargs; i++)
00052                      objp->oargs.sarg[i] = savestr(getstr(sbuf, fp));
00053        } else
00054               objp->oargs.sarg = NULL;
00055 #ifdef IARGS
00056        if ((objp->oargs.niargs = getint(2, fp)) > 0) {
00057               objp->oargs.iarg = (long *)malloc
00058                             (objp->oargs.niargs*sizeof(long));
00059               if (objp->oargs.iarg == NULL)
00060                      goto memerr;
00061               for (i = 0; i < objp->oargs.niargs; i++)
00062                      objp->oargs.iarg[i] = getint(4, fp);
00063        } else
00064               objp->oargs.iarg = NULL;
00065 #endif
00066        if ((objp->oargs.nfargs = getint(2, fp)) > 0) {
00067               objp->oargs.farg = (RREAL *)malloc
00068                             (objp->oargs.nfargs*sizeof(RREAL));
00069               if (objp->oargs.farg == NULL)
00070                      goto memerr;
00071               for (i = 0; i < objp->oargs.nfargs; i++)
00072                      objp->oargs.farg[i] = getflt(fp);
00073        } else
00074               objp->oargs.farg = NULL;
00075        if (feof(fp))
00076               error(SYSTEM, "unexpected EOF in getobj");
00077                                           /* initialize */
00078        objp->os = NULL;
00079                                           /* insert */
00080        insertobject(obj);
00081        return(obj);
00082 memerr:
00083        error(SYSTEM, "out of memory in getobj");
00084        return 0; /* pro forma return */
00085 }
00086 
00087 
00088 void
00089 readscene(fp, objsiz)                     /* read binary scene description */
00090 FILE   *fp;
00091 int    objsiz;
00092 {
00093        char  sbuf[32];
00094        int  i;
00095                                    /* record starting object */
00096        object0 = nobjects;
00097                                    /* read type map */
00098        for (i = 0; getstr(sbuf, fp) != NULL && sbuf[0]; i++)
00099               if ((otypmap[i] = otype(sbuf)) < 0) {
00100                      sprintf(errmsg, "unknown object type \"%s\"",
00101                                    sbuf);
00102                      error(WARNING, errmsg);
00103               }
00104                                    /* read objects */
00105        while (getobj(fp, objsiz) != OVOID)
00106               ;
00107 }
00108 
00109 
00110 static void
00111 putobj(o, fp)               /* write out object */
00112 FILE   *fp;
00113 register OBJREC  *o;
00114 {
00115        register int  i;
00116 
00117        if (o == NULL) {            /* terminator */
00118               putint(-1L, 1, fp);
00119               return;
00120        }
00121        putint((long)o->otype, 1, fp);
00122        putint((long)o->omod, sizeof(OBJECT), fp);
00123        putstr(o->oname, fp);
00124        putint((long)o->oargs.nsargs, 2, fp);
00125        for (i = 0; i < o->oargs.nsargs; i++)
00126               putstr(o->oargs.sarg[i], fp);
00127 #ifdef  IARGS
00128        putint((long)o->oargs.niargs, 2, fp);
00129        for (i = 0; i < o->oargs.niargs; i++)
00130               putint((long)o->oargs.iarg[i], 4, fp);
00131 #endif
00132        putint((long)o->oargs.nfargs, 2, fp);
00133        for (i = 0; i < o->oargs.nfargs; i++)
00134               putflt(o->oargs.farg[i], fp);
00135 }
00136 
00137 
00138 void
00139 writescene(firstobj, nobjs, fp)           /* write binary scene description */
00140 int    firstobj, nobjs;
00141 FILE   *fp;
00142 {
00143        int    i;
00144                                    /* write out type list */
00145        for (i = 0; i < NUMOTYPE; i++)
00146               putstr(ofun[i].funame, fp);
00147        putstr("", fp);
00148                                    /* write objects */
00149        for (i = firstobj; i < firstobj+nobjs; i++)
00150               putobj(objptr(i), fp);
00151        putobj(NULL, fp);           /* terminator */
00152        if (ferror(fp))
00153               error(SYSTEM, "write error in writescene");
00154 }