--- ray/src/common/depthcodec.c 2019/07/26 17:04:12 2.2 +++ ray/src/common/depthcodec.c 2020/01/25 05:35:34 2.8 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: depthcodec.c,v 2.2 2019/07/26 17:04:12 greg Exp $"; +static const char RCSid[] = "$Id: depthcodec.c,v 2.8 2020/01/25 05:35:34 greg Exp $"; #endif /* * Routines to encode/decoded 16-bit depths @@ -8,7 +8,6 @@ static const char RCSid[] = "$Id: depthcodec.c,v 2.2 2 #include "copyright.h" #include -#include #include #include #include "rtio.h" @@ -16,7 +15,7 @@ static const char RCSid[] = "$Id: depthcodec.c,v 2.2 2 #include "depthcodec.h" -#if 0 /* defined as macro in depthcodec.h */ +#ifndef depth2code /* Encode depth as 16-bit signed integer */ int depth2code(double d, double dref) @@ -25,13 +24,14 @@ depth2code(double d, double dref) return -32768; if (d > dref) - return (int)(32768 - 32768*dref/d) - 1; + return (int)(32768.001 - 32768.*dref/d) - 1; - return (int)(32767*d/dref - 32768); + return (int)(32767.*d/dref - 32768.999); } #endif +#ifndef code2depth /* Decode depth from 16-bit signed integer */ double code2depth(int c, double dref) @@ -41,12 +41,13 @@ 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 /* Set codec defaults */ @@ -70,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; @@ -90,7 +97,9 @@ headline(char *s, void *p) } return -1; } - } else if (isview(s)) /* get view params */ + } 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 */ @@ -110,6 +119,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); @@ -126,9 +136,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; } @@ -213,11 +225,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); @@ -285,6 +304,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) {