--- ray/src/rt/ambio.c 2020/11/12 21:25:20 2.14 +++ ray/src/rt/ambio.c 2023/11/15 18:02:52 2.15 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambio.c,v 2.14 2020/11/12 21:25:20 greg Exp $"; +static const char RCSid[] = "$Id: ambio.c,v 2.15 2023/11/15 18:02:52 greg Exp $"; #endif /* * Read and write portable ambient values @@ -13,11 +13,50 @@ static const char RCSid[] = "$Id: ambio.c,v 2.14 2020/ #include "ambient.h" +int *AMB_CNDX = CNDX; /* open ambient file RGBE indices */ +float *AMB_WLPART = WLPART; /* open ambient file limits+partitions (nm) */ + + #define badflt(x) (((x) < -FHUGE) | ((x) > FHUGE)) #define badvec(v) (badflt((v)[0]) | badflt((v)[1]) | badflt((v)[2])) +int +amb_headline( /* check ambient headline (copy to file if *p) */ + char *hl, + void *p +) +{ + static int ambcndx[4]; + static float ambwlpart[4]; + FILE *fout = (FILE *)p; + char fmt[MAXFMTLEN]; + + if (formatval(fmt, hl)) { + if (strcmp(fmt, AMBFMT)) + return(-1); + return(0); + } + if (isncomp(hl)) { + ambcndx[3] = ncompval(hl); + if ((ambcndx[3] < 3) | (ambcndx[3] > MAXCSAMP)) + return(-1); + AMB_CNDX = ambcndx; + return(1); + } + if (iswlsplit(hl)) { + if (!wlsplitval(ambwlpart, hl)) + return(-1); + AMB_WLPART = ambwlpart; + return(1); + } + if (fout) + fputs(hl, fout); + return(0); +} + + void putambmagic(fp) /* write out ambient value magic number */ FILE *fp; @@ -53,20 +92,27 @@ writambval( /* write ambient value to stream */ FILE *fp ) { - COLR clr; + SCOLR sclr; putint(av->lvl, 1, fp); putflt(av->weight, fp); putpos(av->pos, fp); putint(av->ndir, sizeof(av->ndir), fp); putint(av->udir, sizeof(av->udir), fp); - setcolr(clr, colval(av->val,RED), - colval(av->val,GRN), colval(av->val,BLU)); - putbinary((char *)clr, sizeof(clr), 1, fp); putv2(av->rad, fp); putv2(av->gpos, fp); putv2(av->gdir, fp); putint(av->corral, sizeof(av->corral), fp); + if ((AMB_CNDX == CNDX) & (AMB_WLPART == WLPART)) { + scolor_scolr(sclr, av->val); + } else { + SCOLOR scol; + convertscolor(scol, AMB_CNDX[3], AMB_WLPART[0], AMB_WLPART[3], + av->val, NCSAMP, WLPART[0], WLPART[3]); + scolor2scolr(sclr, scol, AMB_CNDX[3]); + } + putbinary(sclr, AMB_CNDX[3]+1, 1, fp); + return(ferror(fp) ? -1 : 0); } @@ -76,7 +122,7 @@ ambvalOK( /* check consistency of ambient value */ AMBVAL *av ) { - double d; + int i; if (badvec(av->pos)) return(0); if (!av->ndir | !av->udir) return(0); @@ -84,14 +130,11 @@ ambvalOK( /* check consistency of ambient value */ if ((av->rad[0] <= 0.) | (av->rad[0] >= FHUGE)) return(0); if ((av->rad[1] <= 0.) | (av->rad[1] >= FHUGE)) return(0); if (av->rad[0] > av->rad[1]+FTINY) return(0); - if ((colval(av->val,RED) < 0.) | - (colval(av->val,RED) >= FHUGE) | - (colval(av->val,GRN) < 0.) | - (colval(av->val,GRN) >= FHUGE) | - (colval(av->val,BLU) < 0.) | - (colval(av->val,BLU) >= FHUGE)) return(0); if (badflt(av->gpos[0]) | badflt(av->gpos[1])) return(0); if (badflt(av->gdir[0]) | badflt(av->gdir[1])) return(0); + for (i = AMB_CNDX[3]; i-- > 0; ) + if ((av->val[i] < 0.) | (av->val[i] >= FHUGE)) + return(0); return(1); } @@ -102,7 +145,7 @@ readambval( /* read ambient value from stream */ FILE *fp ) { - COLR clr; + SCOLR sclr; av->lvl = getint(1, fp) & 0xff; if (feof(fp)) @@ -111,12 +154,19 @@ readambval( /* read ambient value from stream */ getpos(av->pos, fp); av->ndir = getint(sizeof(av->ndir), fp); av->udir = getint(sizeof(av->udir), fp); - if (getbinary((char *)clr, sizeof(clr), 1, fp) != 1) - return(0); - colr_color(av->val, clr); getv2(av->rad, fp); getv2(av->gpos, fp); getv2(av->gdir, fp); av->corral = (uint32)getint(sizeof(av->corral), fp); + if (getbinary(sclr, AMB_CNDX[3]+1, 1, fp) != 1) + return(0); + if ((AMB_CNDX == CNDX) & (AMB_WLPART == WLPART)) { + scolr_scolor(av->val, sclr); + } else { + SCOLOR scol; + scolr2scolor(scol, sclr, AMB_CNDX[3]); + convertscolor(av->val, NCSAMP, WLPART[0], WLPART[3], + scol, AMB_CNDX[3], AMB_WLPART[0], AMB_WLPART[3]); + } return(feof(fp) ? 0 : ambvalOK(av)); }