ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/depthcodec.c
(Generate patch)

Comparing ray/src/common/depthcodec.c (file contents):
Revision 2.2 by greg, Fri Jul 26 17:04:12 2019 UTC vs.
Revision 2.13 by greg, Thu Sep 22 21:45:28 2022 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   #include "copyright.h"
9  
10   #include <stdlib.h>
11 #include <string.h>
11   #include <math.h>
12   #include <ctype.h>
13   #include "rtio.h"
# Line 16 | Line 15 | static const char RCSid[] = "$Id$";
15   #include "depthcodec.h"
16  
17  
18 < #if 0                   /* defined as macro in depthcodec.h */
18 > #ifndef depth2code
19   /* Encode depth as 16-bit signed integer */
20   int
21   depth2code(double d, double dref)
# Line 25 | Line 24 | depth2code(double d, double dref)
24                  return -32768;
25  
26          if (d > dref)
27 <                return (int)(32768 - 32768*dref/d) - 1;
27 >                return (int)(32768.001 - 32768.*dref/d) - 1;
28  
29 <        return (int)(32767*d/dref - 32768);
29 >        return (int)(32767.*d/dref - 32768.999);
30   }
31   #endif
32  
33  
34 + #ifndef code2depth
35   /* Decode depth from 16-bit signed integer */
36   double
37   code2depth(int c, double dref)
# Line 41 | Line 41 | code2depth(int c, double dref)
41  
42          if (c >= 32767)
43                  return FHUGE;
44 <                
45 <        if (c < 0)
46 <                return dref*(32767.5 + c)*(1./32767.);
47 <        
48 <        return dref*32768./(32766.5 - c);
44 >
45 >        if (c >= -1)
46 >                return dref*32768./(32766.5 - c);
47 >
48 >        return dref*(32768.5 + c)*(1./32767.);
49   }
50 + #endif
51  
52  
53   /* Set codec defaults */
# Line 70 | Line 71 | static int
71   headline(char *s, void *p)
72   {
73          DEPTHCODEC      *dcp = (DEPTHCODEC *)p;
74 +        int             rv;
75  
76          if (formatval(dcp->inpfmt, s))  /* don't pass format */
77                  return 0;
78 <                                        /* check for reference depth */
78 >
79 >        if ((rv = isbigendian(s)) >= 0) {
80 >                dcp->swapped = (nativebigendian() != rv);
81 >                return 0;
82 >        }
83 >        if (!strncmp(s, "NCOMP=", 6)) {
84 >                if (atoi(s+6) != 1) {
85 >                        if (dcp->hdrflags & HF_STDERR) {
86 >                                fputs(dcp->inpname, stderr);
87 >                                fputs(": NCOMP must equal 1\n", stderr);
88 >                        }
89 >                        return -1;
90 >                }
91 >                return 0;
92 >        }
93 >        if (!strncmp(s, "NROWS=", 6)) {
94 >                dcp->res.yr = atoi(s+6);
95 >                return 0;
96 >        }
97 >        if (!strncmp(s, "NCOLS=", 6)) {
98 >                dcp->res.xr = atoi(s+6);
99 >                return 0;
100 >        }
101 >                                                /* check for reference depth */
102          if (!strncmp(s, DEPTHSTR, LDEPTHSTR)) {
103                  char    *cp;
104                  strlcpy(dcp->depth_unit, s+LDEPTHSTR, sizeof(dcp->depth_unit));
# Line 90 | Line 115 | headline(char *s, void *p)
115                          }
116                          return -1;
117                  }
118 <        } else if (isview(s))           /* get view params */
118 >                if (dcp->hdrflags & HF_ENCODE)
119 >                        return 0;       /* will add this later */
120 >        } else if (!strncmp(s, "SAMP360=", 8))
121 >                dcp->gotview--;
122 >        else if (isview(s))             /* get view params */
123                  dcp->gotview += (sscanview(&dcp->vw, s) > 0);
124          if (dcp->hdrflags & HF_HEADOUT)
125                  fputs(s, stdout);       /* copy to standard output */
# Line 110 | Line 139 | process_dc_header(DEPTHCODEC *dcp, int ac, char *av[])
139                  }
140                  return 0;
141          }
142 +        dcp->gotview *= (dcp->gotview > 0);
143 +                                        /* get resolution string? */
144 +        if (dcp->hdrflags & HF_RESIN &&
145 +                        (dcp->res.xr <= 0) | (dcp->res.yr <= 0) &&
146 +                        !fgetsresolu(&dcp->res, dcp->finp)) {
147 +                if (dcp->hdrflags & HF_STDERR) {
148 +                        fputs(dcp->inpname, stderr);
149 +                        fputs(": bad resolution string\n", stderr);
150 +                }
151 +                return 0;
152 +        }
153          if (dcp->hdrflags & HF_HEADOUT) {       /* finish header */
154                  if (!(dcp->hdrflags & HF_HEADIN))
155                          newheader("RADIANCE", stdout);
# Line 120 | Line 160 | process_dc_header(DEPTHCODEC *dcp, int ac, char *av[])
160                          fputs(dcp->depth_unit, stdout);
161                          fputc('\n', stdout);
162                          fputformat(DEPTH16FMT, stdout);
163 <                } else
163 >                } else {
164 >                        fputs("NCOMP=1\n", stdout);
165 >                        if ((dcp->hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESIN)
166 >                                printf("NCOLS=%d\nNROWS=%d\n",
167 >                                                scanlen(&dcp->res),
168 >                                                numscans(&dcp->res));
169                          switch (dcp->format) {
170                          case 'a':
171                                  fputformat("ascii", stdout);
172                                  break;
173                          case 'f':
174 +                                fputendian(stdout);
175                                  fputformat("float", stdout);
176                                  break;
177                          case 'd':
178 +                                fputendian(stdout);
179                                  fputformat("double", stdout);
180                                  break;
181                          }
182 +                }
183                  fputc('\n', stdout);
184          }
185 <                                        /* get/put resolution string */
138 <        if (dcp->hdrflags & HF_RESIN && !fgetsresolu(&dcp->res, dcp->finp)) {
139 <                if (dcp->hdrflags & HF_STDERR) {
140 <                        fputs(dcp->inpname, stderr);
141 <                        fputs(": bad resolution string\n", stderr);
142 <                }
143 <                return 0;
144 <        }
145 <        if (dcp->hdrflags & HF_RESOUT)
185 >        if (dcp->hdrflags & HF_RESOUT)  /* put resolution string? */
186                  fputsresolu(&dcp->res, stdout);
187  
188          dcp->dstart = dcp->curpos = ftell(dcp->finp);
# Line 213 | Line 253 | check_decode_worldpos(DEPTHCODEC *dcp)
253   double
254   decode_depth_next(DEPTHCODEC *dcp)
255   {
256 <        int     c = getint(2, dcp->finp);
256 >        int     c;
257  
258 +        if (dcp->use_last) {
259 +                dcp->use_last = 0;
260 +                return code2depth(dcp->last_dc, dcp->refdepth);
261 +        }
262 +        c = getint(2, dcp->finp);
263 +
264          if (c == EOF && feof(dcp->finp))
265                  return -1.;
266  
267 +        dcp->last_dc = c;
268          dcp->curpos += 2;
269  
270          return code2depth(c, dcp->refdepth);
# Line 231 | Line 278 | compute_worldpos(FVECT wpos, DEPTHCODEC *dcp, int x, i
278          RREAL   loc[2];
279          FVECT   rdir;
280  
281 +        if (d >= FHUGE*.99)
282 +                goto badval;
283 +
284          pix2loc(loc, &dcp->res, x, y);
285  
286 <        if (viewray(wpos, rdir, &dcp->vw, loc[0], loc[1]) < -FTINY) {
287 <                VCOPY(wpos, dcp->vw.vp);
288 <                return 0;
286 >        if (viewray(wpos, rdir, &dcp->vw, loc[0], loc[1]) >= -FTINY) {
287 >                VSUM(wpos, wpos, rdir, d);
288 >                return 1;
289          }
290 <        VSUM(wpos, wpos, rdir, d);
291 <        return 1;
290 > badval:
291 >        VCOPY(wpos, dcp->vw.vp);
292 >        return 0;
293   }
294  
295  
# Line 285 | Line 336 | seek_dc_pix(DEPTHCODEC *dcp, int x, int y)
336          }
337          seekpos = dcp->dstart + 2*((long)y*scanlen(&dcp->res) + x);
338  
339 +        if (seekpos == dcp->curpos-2) {
340 +                dcp->use_last++;        /* avoids seek/read */
341 +                return 1;
342 +        }
343          if (seekpos != dcp->curpos &&
344                          fseek(dcp->finp, seekpos, SEEK_SET) == EOF) {
345                  if (dcp->hdrflags & HF_STDERR) {
# Line 294 | Line 349 | seek_dc_pix(DEPTHCODEC *dcp, int x, int y)
349                  return -1;
350          }
351          dcp->curpos = seekpos;
352 +        dcp->use_last = 0;
353          return 1;
354   }
355  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines