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

Comparing ray/src/rt/rpict.c (file contents):
Revision 1.10 by greg, Tue Oct 3 12:17:09 1989 UTC vs.
Revision 1.20 by greg, Tue Mar 27 11:40:07 1990 UTC

# Line 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   #ifdef BSD
16   #include  <sys/time.h>
17   #include  <sys/resource.h>
18 + #else
19 + #include  <signal.h>
20   #endif
21 + #include  <fcntl.h>
22  
23   #include  "view.h"
24  
25   #include  "random.h"
26  
27 < VIEW  ourview = STDVIEW(512);           /* view parameters */
27 > VIEW  ourview = STDVIEW;                /* view parameters */
28 > int  hresolu = 512;                     /* horizontal resolution */
29 > int  vresolu = 512;                     /* vertical resolution */
30 > double  pixaspect = 1.0;                /* pixel aspect ratio */
31  
32   int  psample = 4;                       /* pixel sample size */
33   double  maxdiff = .05;                  /* max. difference for interpolation */
# Line 53 | Line 59 | extern long  nrays;                    /* number of rays traced */
59  
60   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
61  
62 + double  pixvalue();
63  
64 +
65   quit(code)                      /* quit program */
66   int  code;
67   {
# Line 80 | Line 88 | report()               /* report progress */
88          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
89                          nrays, pctdone, t/3600.0);
90   #else
91 +        signal(SIGALRM, report);
92          sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
93   #endif
94          eputs(errmsg);
# Line 89 | Line 98 | report()               /* report progress */
98   }
99  
100  
101 < render(oldfile)                         /* render the scene */
102 < char  *oldfile;
101 > render(zfile, oldfile)                          /* render the scene */
102 > char  *zfile, *oldfile;
103   {
104 +        extern long  lseek();
105          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
106 +        float  *zbar[MAXDIV+1];         /* z values */
107          int  ypos;                      /* current scanline */
108 +        int  ystep;                     /* current y step size */
109 +        int  zfd;
110          COLOR  *colptr;
111 +        float  *zptr;
112          register int  i;
113                                          /* check sampling */
114          if (psample < 1)
# Line 103 | Line 117 | char  *oldfile;
117                  psample = MAXDIV;
118                                          /* allocate scanlines */
119          for (i = 0; i <= psample; i++) {
120 <                scanbar[i] = (COLOR *)malloc(ourview.hresolu*sizeof(COLOR));
120 >                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
121                  if (scanbar[i] == NULL)
122 <                        error(SYSTEM, "out of memory in render");
122 >                        goto memerr;
123          }
124 +                                        /* open z file */
125 +        if (zfile != NULL) {
126 +                if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
127 +                        sprintf(errmsg, "cannot open z file \"%s\"", zfile);
128 +                        error(SYSTEM, errmsg);
129 +                }
130 +                for (i = 0; i <= psample; i++) {
131 +                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
132 +                        if (zbar[i] == NULL)
133 +                                goto memerr;
134 +                }
135 +        } else {
136 +                zfd = -1;
137 +                for (i = 0; i <= psample; i++)
138 +                        zbar[i] = NULL;
139 +        }
140                                          /* write out boundaries */
141 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
141 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
142                                          /* recover file and compute first */
143 <        ypos = ourview.vresolu-1 - salvage(oldfile);
144 <        fillscanline(scanbar[0], ourview.hresolu, ypos, psample);
143 >        i = salvage(oldfile);
144 >        if (zfd != -1 && lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
145 >                error(SYSTEM, "z file seek error in render");
146 >        ypos = vresolu-1 - i;
147 >        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
148 >        ystep = psample;
149                                                  /* compute scanlines */
150 <        for (ypos -= psample; ypos >= 0; ypos -= psample) {
151 <        
152 <                pctdone = 100.0*(ourview.vresolu-ypos-psample)/ourview.vresolu;
153 <
154 <                colptr = scanbar[psample];              /* move base to top */
155 <                scanbar[psample] = scanbar[0];
150 >        for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
151 >                                                        /* record progress */
152 >                pctdone = 100.0*(vresolu-1-ypos-ystep)/vresolu;
153 >                                                        /* bottom adjust? */
154 >                if (ypos < 0) {
155 >                        ystep += ypos;
156 >                        ypos = 0;
157 >                }
158 >                colptr = scanbar[ystep];                /* move base to top */
159 >                scanbar[ystep] = scanbar[0];
160                  scanbar[0] = colptr;
161 +                zptr = zbar[ystep];
162 +                zbar[ystep] = zbar[0];
163 +                zbar[0] = zptr;
164                                                          /* fill base line */
165 <                fillscanline(scanbar[0], ourview.hresolu, ypos, psample);
165 >                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
166                                                          /* fill bar */
167 <                fillscanbar(scanbar, ourview.hresolu, ypos, psample);
167 >                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
168                                                          /* write it out */
169 <                for (i = psample; i > 0; i--)
170 <                        if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
169 >                for (i = ystep; i > 0; i--) {
170 >                        if (zfd != -1 && write(zfd, (char *)zbar[i],
171 >                                        hresolu*sizeof(float))
172 >                                        < hresolu*sizeof(float))
173                                  goto writerr;
174 +                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
175 +                                goto writerr;
176 +                }
177                  if (fflush(stdout) == EOF)
178                          goto writerr;
179          }
180 <                                                /* compute residual */
181 <        colptr = scanbar[psample];
182 <        scanbar[psample] = scanbar[0];
183 <        scanbar[0] = colptr;
138 <        if (ypos > -psample) {
139 <                fillscanline(scanbar[-ypos], ourview.hresolu,
140 <                                0, psample);
141 <                fillscanbar(scanbar-ypos, ourview.hresolu,
142 <                                0, psample+ypos);
143 <        }
144 <        for (i = psample; i+ypos >= 0; i--)
145 <                if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
180 >                                                /* clean up */
181 >        if (zfd != -1) {
182 >                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
183 >                                < hresolu*sizeof(float))
184                          goto writerr;
185 +                if (close(zfd) == -1)
186 +                        goto writerr;
187 +                for (i = 0; i <= psample; i++)
188 +                        free((char *)zbar[i]);
189 +        }
190 +        fwritescan(scanbar[0], hresolu, stdout);
191          if (fflush(stdout) == EOF)
192                  goto writerr;
149        pctdone = 100.0;
150                                                /* free scanlines */
193          for (i = 0; i <= psample; i++)
194                  free((char *)scanbar[i]);
195 +        pctdone = 100.0;
196          return;
197   writerr:
198          error(SYSTEM, "write error in render");
199 + memerr:
200 +        error(SYSTEM, "out of memory in render");
201   }
202  
203  
204 < fillscanline(scanline, xres, y, xstep)          /* fill scan line at y */
204 > fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
205   register COLOR  *scanline;
206 + register float  *zline;
207   int  xres, y, xstep;
208   {
209          int  b = xstep;
210 +        double  z;
211          register int  i;
212          
213 <        pixvalue(scanline[0], 0, y);
213 >        z = pixvalue(scanline[0], 0, y);
214 >        if (zline) zline[0] = z;
215  
216 <        for (i = xstep; i < xres; i += xstep) {
217 <        
218 <                pixvalue(scanline[i], i, y);
216 >        for (i = xstep; i < xres-1+xstep; i += xstep) {
217 >                if (i >= xres) {
218 >                        xstep += xres-1-i;
219 >                        i = xres-1;
220 >                }
221 >                z = pixvalue(scanline[i], i, y);
222 >                if (zline) zline[i] = z;
223                  
224 <                b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2);
224 >                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
225 >                                i-xstep, y, xstep, 0, b/2);
226          }
174        if (i-xstep < xres-1) {
175                pixvalue(scanline[xres-1], xres-1, y);
176                fillsample(scanline+i-xstep, i-xstep, y,
177                                xres-1-(i-xstep), 0, b/2);
178        }
227   }
228  
229  
230 < fillscanbar(scanbar, xres, y, ysize)            /* fill interior */
230 > fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
231   register COLOR  *scanbar[];
232 + register float  *zbar[];
233   int  xres, y, ysize;
234   {
235          COLOR  vline[MAXDIV+1];
236 +        float  zline[MAXDIV+1];
237          int  b = ysize;
238 +        double  z;
239          register int  i, j;
240          
241          for (i = 0; i < xres; i++) {
242                  
243                  copycolor(vline[0], scanbar[0][i]);
244                  copycolor(vline[ysize], scanbar[ysize][i]);
245 +                if (zbar[0]) {
246 +                        zline[0] = zbar[0][i];
247 +                        zline[ysize] = zbar[ysize][i];
248 +                }
249                  
250 <                b = fillsample(vline, i, y, 0, ysize, b/2);
250 >                b = fillsample(vline, zbar[0] ? zline : NULL,
251 >                                i, y, 0, ysize, b/2);
252                  
253                  for (j = 1; j < ysize; j++)
254                          copycolor(scanbar[j][i], vline[j]);
255 +                if (zbar[0])
256 +                        for (j = 1; j < ysize; j++)
257 +                                zbar[j][i] = zline[j];
258          }
259   }
260  
261  
262   int
263 < fillsample(colline, x, y, xlen, ylen, b)        /* fill interior points */
263 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
264   register COLOR  *colline;
265 + register float  *zline;
266   int  x, y;
267   int  xlen, ylen;
268   int  b;
269   {
270 +        extern double  fabs();
271          double  ratio;
272 +        double  z;
273          COLOR  ctmp;
274          int  ncut;
275          register int  len;
# Line 220 | Line 282 | int  b;
282          if (len <= 1)                   /* limit recursion */
283                  return(0);
284          
285 <        if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) {
285 >        if (b > 0
286 >        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
287 >                        || bigdiff(colline[0], colline[len], maxdiff)) {
288          
289 <                pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
289 >                z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
290 >                if (zline) zline[len>>1] = z;
291                  ncut = 1;
292                  
293          } else {                                        /* interpolate */
# Line 230 | Line 295 | int  b;
295                  copycolor(colline[len>>1], colline[len]);
296                  ratio = (double)(len>>1) / len;
297                  scalecolor(colline[len>>1], ratio);
298 <                copycolor(ctmp, colline[0]);
298 >                if (zline) zline[len>>1] = zline[len] * ratio;
299                  ratio = 1.0 - ratio;
300 +                copycolor(ctmp, colline[0]);
301                  scalecolor(ctmp, ratio);
302                  addcolor(colline[len>>1], ctmp);
303 +                if (zline) zline[len>>1] += zline[0] * ratio;
304                  ncut = 0;
305          }
306                                                          /* recurse */
307 <        ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2);
307 >        ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
308          
309 <        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1),
310 <                                xlen - (xlen>>1), ylen - (ylen>>1), b/2);
309 >        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
310 >                        x+(xlen>>1), y+(ylen>>1),
311 >                        xlen-(xlen>>1), ylen-(ylen>>1), b/2);
312  
313          return(ncut);
314   }
315  
316  
317 + double
318   pixvalue(col, x, y)             /* compute pixel value */
319   COLOR  col;                     /* returned color */
320   int  x, y;                      /* pixel position */
321   {
322          static RAY  thisray;    /* our ray for this pixel */
323  
324 <        rayview(thisray.rorg, thisray.rdir, &ourview,
325 <                        x + pixjitter(), y + pixjitter());
324 >        viewray(thisray.rorg, thisray.rdir, &ourview,
325 >                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
326  
327          rayorigin(&thisray, NULL, PRIMARY, 1.0);
328          
329          rayvalue(&thisray);                     /* trace ray */
330  
331          copycolor(col, thisray.rcol);           /* return color */
332 +        
333 +        return(thisray.rt);                     /* return distance */
334   }
335  
336  
# Line 289 | Line 360 | char  *oldfile;
360                  return(0);
361          }
362  
363 <        if (x != ourview.hresolu || y != ourview.vresolu) {
363 >        if (x != hresolu || y != vresolu) {
364                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
365                                  oldfile);
366                  error(USER, errmsg);
367          }
368  
369 <        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR));
369 >        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
370          if (scanline == NULL)
371                  error(SYSTEM, "out of memory in salvage");
372 <        for (y = 0; y < ourview.vresolu; y++) {
373 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
372 >        for (y = 0; y < vresolu; y++) {
373 >                if (freadcolrs(scanline, hresolu, fp) < 0)
374                          break;
375 <                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0)
375 >                if (fwritecolrs(scanline, hresolu, stdout) < 0)
376                          goto writerr;
377          }
378          if (fflush(stdout) == EOF)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines