--- ray/src/common/depthcodec.c 2019/07/26 18:52:32 2.3 +++ ray/src/common/depthcodec.c 2021/01/26 18:47:25 2.11 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: depthcodec.c,v 2.3 2019/07/26 18:52:32 greg Exp $"; +static const char RCSid[] = "$Id: depthcodec.c,v 2.11 2021/01/26 18:47:25 greg Exp $"; #endif /* * Routines to encode/decoded 16-bit depths @@ -8,7 +8,6 @@ static const char RCSid[] = "$Id: depthcodec.c,v 2.3 2 #include "copyright.h" #include -#include #include #include #include "rtio.h" @@ -27,7 +26,7 @@ depth2code(double d, double dref) if (d > dref) return (int)(32768.001 - 32768.*dref/d) - 1; - return (int)(32767.*d/dref - 32768.); + return (int)(32767.*d/dref - 32768.999); } #endif @@ -42,11 +41,11 @@ code2depth(int c, double dref) if (c >= 32767) return FHUGE; - - if (c < 0) - return dref*(32767.5 + c)*(1./32767.); - - return dref*32768./(32766.5 - c); + + if (c >= -1) + return dref*32768./(32766.5 - c); + + return dref*(32768.5 + c)*(1./32767.); } #endif @@ -72,9 +71,15 @@ static int headline(char *s, void *p) { DEPTHCODEC *dcp = (DEPTHCODEC *)p; + int rv; if (formatval(dcp->inpfmt, s)) /* don't pass format */ return 0; + + if ((rv = isbigendian(s)) >= 0) { + dcp->swapped = (nativebigendian() != rv); + return 0; + } /* check for reference depth */ if (!strncmp(s, DEPTHSTR, LDEPTHSTR)) { char *cp; @@ -92,7 +97,11 @@ headline(char *s, void *p) } return -1; } - } else if (isview(s)) /* get view params */ + if (dcp->hdrflags & HF_ENCODE) + return 0; /* will add this later */ + } else if (!strncmp(s, "SAMP360=", 8)) + dcp->gotview--; + else if (isview(s)) /* get view params */ dcp->gotview += (sscanview(&dcp->vw, s) > 0); if (dcp->hdrflags & HF_HEADOUT) fputs(s, stdout); /* copy to standard output */ @@ -112,6 +121,7 @@ process_dc_header(DEPTHCODEC *dcp, int ac, char *av[]) } return 0; } + dcp->gotview *= (dcp->gotview > 0); if (dcp->hdrflags & HF_HEADOUT) { /* finish header */ if (!(dcp->hdrflags & HF_HEADIN)) newheader("RADIANCE", stdout); @@ -128,9 +138,11 @@ process_dc_header(DEPTHCODEC *dcp, int ac, char *av[]) fputformat("ascii", stdout); break; case 'f': + fputendian(stdout); fputformat("float", stdout); break; case 'd': + fputendian(stdout); fputformat("double", stdout); break; } @@ -215,11 +227,18 @@ check_decode_worldpos(DEPTHCODEC *dcp) double decode_depth_next(DEPTHCODEC *dcp) { - int c = getint(2, dcp->finp); + int c; + if (dcp->use_last) { + dcp->use_last = 0; + return code2depth(dcp->last_dc, dcp->refdepth); + } + c = getint(2, dcp->finp); + if (c == EOF && feof(dcp->finp)) return -1.; + dcp->last_dc = c; dcp->curpos += 2; return code2depth(c, dcp->refdepth); @@ -233,14 +252,18 @@ compute_worldpos(FVECT wpos, DEPTHCODEC *dcp, int x, i RREAL loc[2]; FVECT rdir; + if (d >= FHUGE*.99) + goto badval; + pix2loc(loc, &dcp->res, x, y); - if (viewray(wpos, rdir, &dcp->vw, loc[0], loc[1]) < -FTINY) { - VCOPY(wpos, dcp->vw.vp); - return 0; + if (viewray(wpos, rdir, &dcp->vw, loc[0], loc[1]) >= -FTINY) { + VSUM(wpos, wpos, rdir, d); + return 1; } - VSUM(wpos, wpos, rdir, d); - return 1; +badval: + VCOPY(wpos, dcp->vw.vp); + return 0; } @@ -287,6 +310,10 @@ seek_dc_pix(DEPTHCODEC *dcp, int x, int y) } seekpos = dcp->dstart + 2*((long)y*scanlen(&dcp->res) + x); + if (seekpos == dcp->curpos-2) { + dcp->use_last++; /* avoids seek/read */ + return 1; + } if (seekpos != dcp->curpos && fseek(dcp->finp, seekpos, SEEK_SET) == EOF) { if (dcp->hdrflags & HF_STDERR) { @@ -296,6 +323,7 @@ seek_dc_pix(DEPTHCODEC *dcp, int x, int y) return -1; } dcp->curpos = seekpos; + dcp->use_last = 0; return 1; }