--- ray/src/common/depthcodec.c 2019/07/26 17:04:12 2.2 +++ ray/src/common/depthcodec.c 2025/06/07 05:09:45 2.14 @@ -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.14 2025/06/07 05:09:45 greg Exp $"; #endif /* * Routines to encode/decoded 16-bit depths @@ -7,16 +7,15 @@ static const char RCSid[] = "$Id: depthcodec.c,v 2.2 2 #include "copyright.h" -#include -#include #include #include +#include "paths.h" #include "rtio.h" #include "fvect.h" #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,10 +71,34 @@ static int headline(char *s, void *p) { DEPTHCODEC *dcp = (DEPTHCODEC *)p; + int rv; if (formatval(dcp->inpfmt, s)) /* don't pass format */ return 0; - /* check for reference depth */ + + if ((rv = isbigendian(s)) >= 0) { + dcp->swapped = (nativebigendian() != rv); + return 0; + } + if (!strncmp(s, "NCOMP=", 6)) { + if (atoi(s+6) != 1) { + if (dcp->hdrflags & HF_STDERR) { + fputs(dcp->inpname, stderr); + fputs(": NCOMP must equal 1\n", stderr); + } + return -1; + } + return 0; + } + if (!strncmp(s, "NROWS=", 6)) { + dcp->res.yr = atoi(s+6); + return 0; + } + if (!strncmp(s, "NCOLS=", 6)) { + dcp->res.xr = atoi(s+6); + return 0; + } + /* check for reference depth */ if (!strncmp(s, DEPTHSTR, LDEPTHSTR)) { char *cp; strlcpy(dcp->depth_unit, s+LDEPTHSTR, sizeof(dcp->depth_unit)); @@ -90,7 +115,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 */ @@ -110,6 +139,17 @@ process_dc_header(DEPTHCODEC *dcp, int ac, char *av[]) } return 0; } + dcp->gotview *= (dcp->gotview > 0); + /* get resolution string? */ + if (dcp->hdrflags & HF_RESIN && + (dcp->res.xr <= 0) | (dcp->res.yr <= 0) && + !fgetsresolu(&dcp->res, dcp->finp)) { + if (dcp->hdrflags & HF_STDERR) { + fputs(dcp->inpname, stderr); + fputs(": bad resolution string\n", stderr); + } + return 0; + } if (dcp->hdrflags & HF_HEADOUT) { /* finish header */ if (!(dcp->hdrflags & HF_HEADIN)) newheader("RADIANCE", stdout); @@ -120,29 +160,29 @@ process_dc_header(DEPTHCODEC *dcp, int ac, char *av[]) fputs(dcp->depth_unit, stdout); fputc('\n', stdout); fputformat(DEPTH16FMT, stdout); - } else + } else { + fputs("NCOMP=1\n", stdout); + if ((dcp->hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESIN) + printf("NCOLS=%d\nNROWS=%d\n", + scanlen(&dcp->res), + numscans(&dcp->res)); switch (dcp->format) { case 'a': fputformat("ascii", stdout); break; case 'f': + fputendian(stdout); fputformat("float", stdout); break; case 'd': + fputendian(stdout); fputformat("double", stdout); break; } + } fputc('\n', stdout); } - /* get/put resolution string */ - if (dcp->hdrflags & HF_RESIN && !fgetsresolu(&dcp->res, dcp->finp)) { - if (dcp->hdrflags & HF_STDERR) { - fputs(dcp->inpname, stderr); - fputs(": bad resolution string\n", stderr); - } - return 0; - } - if (dcp->hdrflags & HF_RESOUT) + if (dcp->hdrflags & HF_RESOUT) /* put resolution string? */ fputsresolu(&dcp->res, stdout); dcp->dstart = dcp->curpos = ftell(dcp->finp); @@ -213,11 +253,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); @@ -231,14 +278,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; } @@ -285,6 +336,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) { @@ -294,6 +349,7 @@ seek_dc_pix(DEPTHCODEC *dcp, int x, int y) return -1; } dcp->curpos = seekpos; + dcp->use_last = 0; return 1; }