ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.21
Committed: Wed Jan 20 15:19:47 1993 UTC (31 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.20: +5 -39 lines
Log Message:
added -P and -PP persist options

File Contents

# User Rev Content
1 greg 2.8 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * rpict.c - routines and variables for picture generation.
9     *
10     * 8/14/85
11     */
12    
13     #include "ray.h"
14    
15     #ifdef BSD
16     #include <sys/time.h>
17     #include <sys/resource.h>
18 greg 1.34 #endif
19    
20 greg 1.14 #include <signal.h>
21 greg 1.1
22     #include "view.h"
23    
24 greg 1.36 #include "resolu.h"
25    
26 greg 1.1 #include "random.h"
27    
28 greg 2.12 #include "paths.h"
29    
30 greg 2.21 #define RFTEMPLATE "rfXXXXXX"
31    
32 greg 1.25 int dimlist[MAXDIM]; /* sampling dimensions */
33     int ndims = 0; /* number of sampling dimensions */
34     int samplendx; /* sample index number */
35    
36 greg 1.12 VIEW ourview = STDVIEW; /* view parameters */
37     int hresolu = 512; /* horizontal resolution */
38     int vresolu = 512; /* vertical resolution */
39 greg 2.14 double pixaspect = 1.0; /* pixel aspect ratio */
40 greg 1.1
41     int psample = 4; /* pixel sample size */
42 greg 2.14 double maxdiff = .05; /* max. difference for interpolation */
43     double dstrpix = 0.67; /* square pixel distribution */
44 greg 1.1
45 greg 2.14 double dstrsrc = 0.0; /* square source distribution */
46     double shadthresh = .05; /* shadow threshold */
47     double shadcert = .5; /* shadow certainty */
48 greg 1.35 int directrelay = 1; /* number of source relays */
49 greg 1.29 int vspretest = 512; /* virtual source pretest density */
50 greg 2.20 int directvis = 1; /* sources visible? */
51 greg 2.14 double srcsizerat = .25; /* maximum ratio source size/dist. */
52 greg 1.1
53 greg 2.14 double specthresh = .15; /* specular sampling threshold */
54     double specjitter = 1.; /* specular sampling jitter */
55 greg 2.5
56 greg 1.1 int maxdepth = 6; /* maximum recursion depth */
57 greg 2.14 double minweight = 5e-3; /* minimum ray weight */
58 greg 1.1
59     COLOR ambval = BLKCOLOR; /* ambient value */
60 greg 2.14 double ambacc = 0.2; /* ambient accuracy */
61 greg 1.6 int ambres = 32; /* ambient resolution */
62 greg 1.1 int ambdiv = 128; /* ambient divisions */
63     int ambssamp = 0; /* ambient super-samples */
64     int ambounce = 0; /* ambient bounces */
65     char *amblist[128]; /* ambient include/exclude list */
66     int ambincl = -1; /* include == 1, exclude == 0 */
67    
68 greg 2.16 #ifdef MSDOS
69     int ralrm = 60; /* seconds between reports */
70     #else
71 greg 1.1 int ralrm = 0; /* seconds between reports */
72 greg 2.16 #endif
73 greg 1.1
74 greg 2.14 double pctdone = 0.0; /* percentage done */
75 greg 1.1
76 greg 1.24 long tlastrept = 0L; /* time at last report */
77    
78     extern long time();
79     extern long tstart; /* starting time */
80    
81 greg 1.1 extern long nrays; /* number of rays traced */
82    
83 greg 2.14 #define MAXDIV 16 /* maximum sample size */
84 greg 1.1
85 greg 2.14 #define pixjitter() (.5+dstrpix*(.5-frandom()))
86 greg 1.1
87 greg 2.8 static int hres, vres; /* resolution for this frame */
88    
89     extern char *mktemp();
90    
91 greg 2.14 double pixvalue();
92 greg 1.1
93 greg 1.11
94 greg 1.1 quit(code) /* quit program */
95     int code;
96     {
97 greg 2.8 if (code) /* report status */
98 greg 1.1 report();
99 greg 2.21 headclean(); /* delete header file */
100     pfclean(); /* clean up persist files */
101 greg 1.1 exit(code);
102     }
103    
104    
105 greg 1.24 #ifdef BSD
106 greg 1.1 report() /* report progress */
107     {
108     struct rusage rubuf;
109 greg 2.14 double t;
110 greg 1.1
111     getrusage(RUSAGE_SELF, &rubuf);
112     t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
113     t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
114     getrusage(RUSAGE_CHILDREN, &rubuf);
115     t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
116     t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
117    
118     sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
119     nrays, pctdone, t/3600.0);
120 greg 1.24 eputs(errmsg);
121     tlastrept = time((long *)0);
122     }
123 greg 1.1 #else
124 greg 1.24 report() /* report progress */
125     {
126     tlastrept = time((long *)0);
127     sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
128     nrays, pctdone, (tlastrept-tstart)/3600.0);
129 greg 1.1 eputs(errmsg);
130 greg 2.4 signal(SIGALRM, report);
131 greg 1.1 }
132 greg 1.24 #endif
133 greg 1.1
134    
135 greg 2.8 rpict(seq, pout, zout, prvr) /* generate image(s) */
136     int seq;
137     char *pout, *zout, *prvr;
138     /*
139     * If seq is greater than zero, then we will render a sequence of
140     * images based on view parameter strings read from the standard input.
141     * If pout is NULL, then all images will be sent to the standard ouput.
142     * If seq is greater than zero and prvr is an integer, then it is the
143     * frame number at which rendering should begin. Preceeding view parameter
144     * strings will be skipped in the input.
145     * If pout and prvr are the same, prvr is renamed to avoid overwriting.
146     * Note that pout and zout should contain %d format specifications for
147     * sequenced file naming.
148     */
149     {
150 greg 2.21 extern char *rindex(), *strncpy(), *strcat(), *strcpy();
151 greg 2.9 char fbuf[128], fbuf2[128];
152     register char *cp;
153 greg 2.14 RESOLU rs;
154     double pa;
155 greg 2.8 /* check sampling */
156     if (psample < 1)
157     psample = 1;
158     else if (psample > MAXDIV) {
159     sprintf(errmsg, "pixel sampling reduced from %d to %d",
160     psample, MAXDIV);
161     error(WARNING, errmsg);
162     psample = MAXDIV;
163     }
164     /* get starting frame */
165     if (seq <= 0)
166     seq = 0;
167     else if (prvr != NULL && isint(prvr)) {
168 greg 2.9 register int rn; /* skip to specified view */
169 greg 2.8 if ((rn = atoi(prvr)) < seq)
170     error(USER, "recover frame less than start frame");
171     if (pout == NULL)
172     error(USER, "missing output file specification");
173     for ( ; seq < rn; seq++)
174     if (nextview(stdin) == EOF)
175     error(USER, "unexpected EOF on view input");
176     prvr = fbuf; /* mark for renaming */
177     }
178 greg 2.18 if (pout != NULL & prvr != NULL) {
179 greg 2.8 sprintf(fbuf, pout, seq);
180 greg 2.18 if (!strcmp(prvr, fbuf)) { /* rename */
181     strcpy(fbuf2, fbuf);
182     for (cp = fbuf2; *cp; cp++)
183     ;
184     while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
185     cp--;
186     strcpy(cp, RFTEMPLATE);
187 greg 2.8 prvr = mktemp(fbuf2);
188     if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
189     sprintf(errmsg,
190     "cannot rename \"%s\" to \"%s\"",
191     fbuf, prvr);
192     error(SYSTEM, errmsg);
193     }
194     }
195     }
196     /* render sequence */
197     do {
198     if (seq && nextview(stdin) == EOF)
199     break;
200     if (pout != NULL) {
201     sprintf(fbuf, pout, seq);
202     if (freopen(fbuf, "w", stdout) == NULL) {
203     sprintf(errmsg,
204     "cannot open output file \"%s\"", fbuf);
205     error(SYSTEM, errmsg);
206     }
207 greg 2.17 #ifdef MSDOS
208     setmode(fileno(stdout), O_BINARY);
209     #endif
210 greg 2.8 dupheader();
211     }
212     hres = hresolu; vres = vresolu; pa = pixaspect;
213     if (prvr != NULL)
214     if (viewfile(prvr, &ourview, &rs) <= 0
215     || rs.or != PIXSTANDARD) {
216     sprintf(errmsg,
217     "cannot recover view parameters from \"%s\"", prvr);
218     error(WARNING, errmsg);
219     } else {
220     pa = 0.0;
221     hres = scanlen(&rs);
222     vres = numscans(&rs);
223     }
224 greg 2.9 if ((cp = setview(&ourview)) != NULL)
225     error(USER, cp);
226 greg 2.8 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
227     if (seq) {
228     if (ralrm > 0) {
229 greg 2.11 fprintf(stderr, "FRAME %d:", seq);
230     fprintview(&ourview, stderr);
231     putc('\n', stderr);
232     fflush(stderr);
233 greg 2.8 }
234     printf("FRAME=%d\n", seq);
235     }
236     fputs(VIEWSTR, stdout);
237     fprintview(&ourview, stdout);
238     putchar('\n');
239     if (pa < .99 || pa > 1.01)
240     fputaspect(pa, stdout);
241     fputformat(COLRFMT, stdout);
242     putchar('\n');
243     if (zout != NULL)
244 greg 2.9 sprintf(cp=fbuf, zout, seq);
245 greg 2.8 else
246 greg 2.9 cp = NULL;
247     render(cp, prvr);
248 greg 2.8 prvr = NULL;
249     } while (seq++);
250     }
251    
252    
253     nextview(fp) /* get next view from fp */
254     FILE *fp;
255     {
256 greg 2.9 char linebuf[256];
257 greg 2.8
258     while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
259 greg 2.9 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
260 greg 2.8 return(0);
261     return(EOF);
262     }
263    
264    
265 greg 1.11 render(zfile, oldfile) /* render the scene */
266     char *zfile, *oldfile;
267 greg 1.1 {
268 greg 1.16 extern long lseek();
269 greg 1.1 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
270 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
271 greg 2.2 char *sampdens; /* previous sample density */
272 greg 1.1 int ypos; /* current scanline */
273 greg 1.15 int ystep; /* current y step size */
274 greg 1.33 int hstep; /* h step size */
275 greg 1.16 int zfd;
276 greg 1.1 COLOR *colptr;
277 greg 1.11 float *zptr;
278 greg 1.1 register int i;
279 greg 1.9 /* allocate scanlines */
280 greg 1.1 for (i = 0; i <= psample; i++) {
281 greg 2.8 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
282 greg 1.1 if (scanbar[i] == NULL)
283 greg 1.11 goto memerr;
284 greg 1.1 }
285 greg 2.2 hstep = (psample*140+49)/99; /* quincunx sampling */
286     ystep = (psample*99+70)/140;
287     if (hstep > 2) {
288 greg 2.8 i = hres/hstep + 2;
289 greg 2.2 if ((sampdens = malloc(i)) == NULL)
290     goto memerr;
291     while (i--)
292     sampdens[i] = hstep;
293     } else
294     sampdens = NULL;
295 greg 1.11 /* open z file */
296     if (zfile != NULL) {
297 greg 1.16 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
298 greg 1.11 sprintf(errmsg, "cannot open z file \"%s\"", zfile);
299     error(SYSTEM, errmsg);
300     }
301 greg 2.17 #ifdef MSDOS
302     setmode(zfd, O_BINARY);
303     #endif
304 greg 1.11 for (i = 0; i <= psample; i++) {
305 greg 2.8 zbar[i] = (float *)malloc(hres*sizeof(float));
306 greg 1.11 if (zbar[i] == NULL)
307     goto memerr;
308     }
309     } else {
310 greg 1.16 zfd = -1;
311 greg 1.11 for (i = 0; i <= psample; i++)
312     zbar[i] = NULL;
313     }
314 greg 1.1 /* write out boundaries */
315 greg 2.8 fprtresolu(hres, vres, stdout);
316 greg 1.10 /* recover file and compute first */
317 greg 1.11 i = salvage(oldfile);
318 greg 1.23 if (zfd != -1 && i > 0 &&
319 greg 2.8 lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
320 greg 1.11 error(SYSTEM, "z file seek error in render");
321 greg 2.8 pctdone = 100.0*i/vres;
322 greg 1.24 if (ralrm > 0) /* report init stats */
323     report();
324 greg 2.14 #ifndef BSD
325 greg 1.32 else
326     #endif
327     signal(SIGALRM, report);
328 greg 2.8 ypos = vres-1 - i;
329     fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
330 greg 1.10 /* compute scanlines */
331 greg 1.15 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
332     /* bottom adjust? */
333     if (ypos < 0) {
334     ystep += ypos;
335     ypos = 0;
336     }
337     colptr = scanbar[ystep]; /* move base to top */
338     scanbar[ystep] = scanbar[0];
339 greg 1.1 scanbar[0] = colptr;
340 greg 1.15 zptr = zbar[ystep];
341     zbar[ystep] = zbar[0];
342 greg 1.11 zbar[0] = zptr;
343 greg 1.10 /* fill base line */
344 greg 2.2 fillscanline(scanbar[0], zbar[0], sampdens,
345 greg 2.8 hres, ypos, hstep);
346 greg 1.10 /* fill bar */
347 greg 2.8 fillscanbar(scanbar, zbar, hres, ypos, ystep);
348 greg 1.10 /* write it out */
349 greg 2.14 #ifndef BSD
350 greg 1.32 signal(SIGALRM, SIG_IGN); /* don't interrupt writes */
351     #endif
352 greg 1.15 for (i = ystep; i > 0; i--) {
353 greg 1.17 if (zfd != -1 && write(zfd, (char *)zbar[i],
354 greg 2.8 hres*sizeof(float))
355     < hres*sizeof(float))
356 greg 1.1 goto writerr;
357 greg 2.8 if (fwritescan(scanbar[i], hres, stdout) < 0)
358 greg 1.11 goto writerr;
359     }
360 greg 1.1 if (fflush(stdout) == EOF)
361     goto writerr;
362 greg 1.24 /* record progress */
363 greg 2.8 pctdone = 100.0*(vres-1-ypos)/vres;
364 greg 1.24 if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
365     report();
366 greg 2.14 #ifndef BSD
367 greg 1.32 else
368     signal(SIGALRM, report);
369     #endif
370 greg 1.1 }
371 greg 1.11 /* clean up */
372 greg 2.7 signal(SIGALRM, SIG_IGN);
373 greg 1.16 if (zfd != -1) {
374 greg 2.8 if (write(zfd, (char *)zbar[0], hres*sizeof(float))
375     < hres*sizeof(float))
376 greg 1.11 goto writerr;
377 greg 1.20 if (close(zfd) == -1)
378     goto writerr;
379 greg 1.11 for (i = 0; i <= psample; i++)
380     free((char *)zbar[i]);
381     }
382 greg 2.8 fwritescan(scanbar[0], hres, stdout);
383 greg 1.10 if (fflush(stdout) == EOF)
384     goto writerr;
385 greg 1.1 for (i = 0; i <= psample; i++)
386     free((char *)scanbar[i]);
387 greg 2.2 if (sampdens != NULL)
388     free(sampdens);
389 greg 1.11 pctdone = 100.0;
390 greg 2.13 if (ralrm > 0)
391     report();
392 greg 1.1 return;
393     writerr:
394     error(SYSTEM, "write error in render");
395 greg 1.11 memerr:
396     error(SYSTEM, "out of memory in render");
397 greg 1.1 }
398    
399    
400 greg 2.2 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
401 greg 2.14 register COLOR *scanline;
402     register float *zline;
403 greg 2.2 register char *sd;
404 greg 1.9 int xres, y, xstep;
405 greg 1.1 {
406 greg 1.33 static int nc = 0; /* number of calls */
407 greg 2.2 int bl = xstep, b = xstep;
408 greg 2.14 double z;
409 greg 1.1 register int i;
410    
411 greg 1.11 z = pixvalue(scanline[0], 0, y);
412     if (zline) zline[0] = z;
413 greg 1.33 /* zig-zag start for quincunx pattern */
414 greg 2.2 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
415 greg 1.15 if (i >= xres) {
416     xstep += xres-1-i;
417     i = xres-1;
418     }
419 greg 1.11 z = pixvalue(scanline[i], i, y);
420     if (zline) zline[i] = z;
421 greg 2.2 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
422 greg 2.3 if (i <= xstep)
423     b = fillsample(scanline, zline, 0, y, i, 0, b/2);
424     else
425     b = fillsample(scanline+i-xstep,
426     zline ? zline+i-xstep : NULL,
427     i-xstep, y, xstep, 0, b/2);
428 greg 2.2 if (sd) *sd++ = nc & 1 ? bl : b;
429     bl = b;
430 greg 1.1 }
431 greg 2.2 if (sd && nc & 1) *sd = bl;
432 greg 1.1 }
433    
434    
435 greg 1.11 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
436 greg 2.14 register COLOR *scanbar[];
437     register float *zbar[];
438 greg 1.9 int xres, y, ysize;
439 greg 1.1 {
440     COLOR vline[MAXDIV+1];
441 greg 1.11 float zline[MAXDIV+1];
442 greg 1.1 int b = ysize;
443     register int i, j;
444    
445 greg 1.8 for (i = 0; i < xres; i++) {
446 greg 1.1
447     copycolor(vline[0], scanbar[0][i]);
448     copycolor(vline[ysize], scanbar[ysize][i]);
449 greg 1.11 if (zbar[0]) {
450     zline[0] = zbar[0][i];
451     zline[ysize] = zbar[ysize][i];
452     }
453 greg 1.1
454 greg 1.11 b = fillsample(vline, zbar[0] ? zline : NULL,
455     i, y, 0, ysize, b/2);
456 greg 1.1
457     for (j = 1; j < ysize; j++)
458     copycolor(scanbar[j][i], vline[j]);
459 greg 1.11 if (zbar[0])
460     for (j = 1; j < ysize; j++)
461     zbar[j][i] = zline[j];
462 greg 1.1 }
463     }
464    
465    
466     int
467 greg 2.14 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
468     register COLOR *colline;
469     register float *zline;
470 greg 1.1 int x, y;
471     int xlen, ylen;
472     int b;
473     {
474 greg 2.14 double ratio;
475     double z;
476 greg 1.1 COLOR ctmp;
477     int ncut;
478     register int len;
479    
480     if (xlen > 0) /* x or y length is zero */
481     len = xlen;
482     else
483     len = ylen;
484    
485     if (len <= 1) /* limit recursion */
486     return(0);
487    
488 greg 1.11 if (b > 0
489     || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
490     || bigdiff(colline[0], colline[len], maxdiff)) {
491 greg 1.1
492 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
493     if (zline) zline[len>>1] = z;
494 greg 1.1 ncut = 1;
495    
496     } else { /* interpolate */
497    
498     copycolor(colline[len>>1], colline[len]);
499     ratio = (double)(len>>1) / len;
500     scalecolor(colline[len>>1], ratio);
501 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
502     ratio = 1.0 - ratio;
503 greg 1.1 copycolor(ctmp, colline[0]);
504     scalecolor(ctmp, ratio);
505     addcolor(colline[len>>1], ctmp);
506 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
507 greg 1.1 ncut = 0;
508     }
509     /* recurse */
510 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
511 greg 1.1
512 greg 1.11 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
513     x+(xlen>>1), y+(ylen>>1),
514     xlen-(xlen>>1), ylen-(ylen>>1), b/2);
515 greg 1.1
516     return(ncut);
517     }
518    
519    
520 greg 1.11 double
521 greg 1.1 pixvalue(col, x, y) /* compute pixel value */
522     COLOR col; /* returned color */
523     int x, y; /* pixel position */
524     {
525 greg 1.25 static RAY thisray;
526 greg 1.1
527 greg 1.22 if (viewray(thisray.rorg, thisray.rdir, &ourview,
528 greg 2.8 (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
529 greg 1.22 setcolor(col, 0.0, 0.0, 0.0);
530     return(0.0);
531     }
532 greg 1.1
533     rayorigin(&thisray, NULL, PRIMARY, 1.0);
534 greg 1.25
535 greg 2.8 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
536 greg 1.25
537 greg 1.1 rayvalue(&thisray); /* trace ray */
538    
539     copycolor(col, thisray.rcol); /* return color */
540 greg 1.11
541 greg 1.20 return(thisray.rt); /* return distance */
542 greg 1.1 }
543    
544    
545     int
546     salvage(oldfile) /* salvage scanlines from killed program */
547     char *oldfile;
548     {
549     COLR *scanline;
550     FILE *fp;
551     int x, y;
552    
553     if (oldfile == NULL)
554     return(0);
555 greg 1.9
556     if ((fp = fopen(oldfile, "r")) == NULL) {
557 greg 1.1 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
558     error(WARNING, errmsg);
559     return(0);
560     }
561 greg 2.17 #ifdef MSDOS
562     setmode(fileno(fp), O_BINARY);
563     #endif
564 greg 1.1 /* discard header */
565 greg 2.19 getheader(fp, NULL, NULL);
566 greg 1.1 /* get picture size */
567 greg 1.36 if (!fscnresolu(&x, &y, fp)) {
568 greg 1.2 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
569     error(WARNING, errmsg);
570 greg 1.1 fclose(fp);
571     return(0);
572     }
573    
574 greg 2.8 if (x != hres || y != vres) {
575 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
576     oldfile);
577     error(USER, errmsg);
578     }
579    
580 greg 2.8 scanline = (COLR *)malloc(hres*sizeof(COLR));
581 greg 1.1 if (scanline == NULL)
582     error(SYSTEM, "out of memory in salvage");
583 greg 2.8 for (y = 0; y < vres; y++) {
584     if (freadcolrs(scanline, hres, fp) < 0)
585 greg 1.1 break;
586 greg 2.8 if (fwritecolrs(scanline, hres, stdout) < 0)
587 greg 1.1 goto writerr;
588     }
589     if (fflush(stdout) == EOF)
590     goto writerr;
591     free((char *)scanline);
592     fclose(fp);
593     unlink(oldfile);
594     return(y);
595     writerr:
596 greg 2.9 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
597     error(SYSTEM, errmsg);
598 greg 1.31 }
599    
600    
601     int
602     pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
603     register int x, y;
604     int xres, yres;
605     {
606     x -= y;
607     while (x < 0)
608     x += xres;
609     return((((x>>2)*yres + y) << 2) + (x & 3));
610 greg 1.1 }