20 |
|
#define vfork fork |
21 |
|
#endif |
22 |
|
|
23 |
< |
#define NSCANS 64 /* number of scanlines to buffer */ |
23 |
> |
#define MAXSBUF 524268 /* maximum total size of scanline buffer */ |
24 |
> |
#define HSIZE 317 /* size of scanline hash table */ |
25 |
> |
#define NRETIRE 16 /* number of scanlines to retire at once */ |
26 |
|
|
27 |
|
int rt_pid = -1; /* process id for rtrace */ |
28 |
|
int fd_tort, fd_fromrt; /* pipe descriptors */ |
30 |
|
FILE *pictfp = NULL; /* picture file pointer */ |
31 |
|
double exposure; /* picture exposure */ |
32 |
|
int pxsiz, pysiz; /* picture dimensions */ |
33 |
< |
int curpos; /* current scanline */ |
34 |
< |
long *scanpos; /* scanline positions */ |
35 |
< |
struct { |
36 |
< |
long lused; /* for LRU replacement */ |
33 |
> |
|
34 |
> |
static int curpos; /* current scanline */ |
35 |
> |
static long *scanpos; /* scanline positions */ |
36 |
> |
|
37 |
> |
typedef struct scan { |
38 |
|
int y; /* scanline position */ |
39 |
< |
COLR *sl; /* scanline contents */ |
40 |
< |
} scan[NSCANS]; /* buffered scanlines */ |
39 |
> |
long lused; /* for LRU replacement */ |
40 |
> |
struct scan *next; /* next in this hash or free list */ |
41 |
> |
/* followed by the scanline data */ |
42 |
> |
} SCAN; /* buffered scanline */ |
43 |
|
|
44 |
+ |
#define scandata(sl) ((COLR *)((sl)+1)) |
45 |
+ |
#define shash(y) ((y)%HSIZE) |
46 |
+ |
|
47 |
+ |
static SCAN *freelist; /* scanline free list */ |
48 |
+ |
static SCAN *hashtab[HSIZE]; /* scanline hash table */ |
49 |
+ |
|
50 |
|
static long ncall = 0L; /* number of calls to getpictscan */ |
51 |
|
static long nread = 0L; /* number of scanlines read */ |
52 |
|
|
53 |
+ |
SCAN *scanretire(); |
54 |
+ |
|
55 |
|
extern long ftell(); |
56 |
|
|
57 |
|
|
58 |
+ |
SCAN * |
59 |
+ |
claimscan(y) /* claim scanline from buffers */ |
60 |
+ |
int y; |
61 |
+ |
{ |
62 |
+ |
int hi = shash(y); |
63 |
+ |
SCAN *slast; |
64 |
+ |
register SCAN *sl; |
65 |
+ |
|
66 |
+ |
for (sl = hashtab[hi]; sl != NULL; sl = sl->next) |
67 |
+ |
if (sl->y == y) /* active scanline */ |
68 |
+ |
return(sl); |
69 |
+ |
for (slast = NULL, sl = freelist; sl != NULL; slast = sl, sl = sl->next) |
70 |
+ |
if (sl->y == -1 || sl->y == y || sl->next == NULL) { |
71 |
+ |
if (slast == NULL) /* remove from free */ |
72 |
+ |
freelist = sl->next; |
73 |
+ |
else |
74 |
+ |
slast->next = sl->next; |
75 |
+ |
if (sl->y == y) { /* reclaim */ |
76 |
+ |
sl->next = hashtab[hi]; |
77 |
+ |
hashtab[hi] = sl; |
78 |
+ |
#ifdef DEBUG |
79 |
+ |
if (verbose) |
80 |
+ |
fprintf(stderr, |
81 |
+ |
"%s: scanline %d reclaimed\n", |
82 |
+ |
progname, y); |
83 |
+ |
#endif |
84 |
+ |
} |
85 |
+ |
return(sl); |
86 |
+ |
} |
87 |
+ |
return(scanretire()); /* need more free scanlines */ |
88 |
+ |
} |
89 |
+ |
|
90 |
+ |
|
91 |
|
COLR * |
92 |
|
getpictscan(y) /* get picture scanline */ |
93 |
|
int y; |
94 |
|
{ |
95 |
< |
int minused; |
95 |
> |
register SCAN *sl; |
96 |
|
register int i; |
97 |
|
/* first check our buffers */ |
98 |
< |
ncall++; |
99 |
< |
minused = 0; |
100 |
< |
for (i = 0; i < NSCANS; i++) { |
101 |
< |
if (scan[i].y == y) { |
102 |
< |
scan[i].lused = ncall; |
103 |
< |
return(scan[i].sl); |
104 |
< |
} |
59 |
< |
if (scan[i].lused < scan[minused].lused) |
60 |
< |
minused = i; |
61 |
< |
} |
62 |
< |
/* not there, read it in */ |
98 |
> |
sl = claimscan(y); |
99 |
> |
if (sl == NULL) |
100 |
> |
memerr("claimscan()"); |
101 |
> |
sl->lused = ncall++; |
102 |
> |
if (sl->y == y) /* scan hit */ |
103 |
> |
return(scandata(sl)); |
104 |
> |
/* else read in replacement */ |
105 |
|
if (scanpos[y] < 0) { /* need to search */ |
106 |
|
for (i = y+1; i < curpos; i++) |
107 |
|
if (scanpos[i] >= 0) { |
112 |
|
} |
113 |
|
while (curpos >= y) { |
114 |
|
scanpos[curpos] = ftell(pictfp); |
115 |
< |
if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0) |
115 |
> |
if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0) |
116 |
|
goto readerr; |
117 |
|
nread++; |
118 |
|
curpos--; |
120 |
|
} else { |
121 |
|
if (curpos != y && fseek(pictfp, scanpos[y], 0) < 0) |
122 |
|
goto seekerr; |
123 |
< |
if (freadcolrs(scan[minused].sl, pxsiz, pictfp) < 0) |
123 |
> |
if (freadcolrs(scandata(sl), pxsiz, pictfp) < 0) |
124 |
|
goto readerr; |
125 |
|
nread++; |
126 |
|
curpos = y-1; |
127 |
|
} |
128 |
< |
scan[minused].lused = ncall; |
129 |
< |
scan[minused].y = y; |
130 |
< |
return(scan[minused].sl); |
128 |
> |
sl->y = y; |
129 |
> |
i = shash(y); /* add to hash list */ |
130 |
> |
sl->next = hashtab[i]; |
131 |
> |
hashtab[i] = sl; |
132 |
> |
return(scandata(sl)); |
133 |
|
readerr: |
134 |
|
fprintf(stderr, "%s: picture read error\n", progname); |
135 |
|
exit(1); |
186 |
|
|
187 |
|
if (compdir(dir, vh, vv) < 0) |
188 |
|
return(-1.0); |
189 |
+ |
npixinvw++; |
190 |
|
if ((res = pict_val(dir)) >= 0.0) |
191 |
|
return(res); |
192 |
< |
if (rt_pid == -1) |
192 |
> |
if (rt_pid == -1) { |
193 |
> |
npixmiss++; |
194 |
|
return(-1.0); |
195 |
+ |
} |
196 |
|
rt_buf[0] = ourview.vp[0]; |
197 |
|
rt_buf[1] = ourview.vp[1]; |
198 |
|
rt_buf[2] = ourview.vp[2]; |
225 |
|
vb[vh+hsize] = -1.0; |
226 |
|
continue; |
227 |
|
} |
228 |
+ |
npixinvw++; |
229 |
|
if ((vb[vh+hsize] = pict_val(dir)) >= 0.0) |
230 |
|
continue; |
231 |
< |
if (rt_pid == -1) /* missing information */ |
231 |
> |
if (rt_pid == -1) { /* missing information */ |
232 |
> |
npixmiss++; |
233 |
|
continue; |
234 |
+ |
} |
235 |
|
/* send to rtrace */ |
236 |
|
if (n >= MAXPIX) { /* flush */ |
237 |
|
rt_compute(rt_buf, n); |
307 |
|
fprintf("%s: bad picture resolution\n", fn); |
308 |
|
exit(1); |
309 |
|
} |
310 |
< |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
261 |
< |
if (scanpos == NULL) |
262 |
< |
memerr("scanline positions"); |
263 |
< |
for (i = pysiz-1; i >= 0; i--) |
264 |
< |
scanpos[i] = -1L; |
265 |
< |
curpos = pysiz-1; |
266 |
< |
for (i = 0; i < NSCANS; i++) { |
267 |
< |
scan[i].lused = -1; |
268 |
< |
scan[i].y = -1; |
269 |
< |
scan[i].sl = (COLR *)malloc(pxsiz*sizeof(COLR)); |
270 |
< |
if (scan[i].sl == NULL) |
271 |
< |
memerr("scanline buffers"); |
272 |
< |
} |
310 |
> |
initscans(); |
311 |
|
} |
312 |
|
|
313 |
|
|
318 |
|
if (pictfp == NULL) |
319 |
|
return; |
320 |
|
fclose(pictfp); |
321 |
< |
free((char *)scanpos); |
284 |
< |
for (i = 0; i < NSCANS; i++) |
285 |
< |
free((char *)scan[i].sl); |
321 |
> |
donescans(); |
322 |
|
pictfp = NULL; |
323 |
|
} |
324 |
|
|
409 |
|
if (cc < 0) |
410 |
|
return(cc); |
411 |
|
return(siz-nrem); |
412 |
+ |
} |
413 |
+ |
|
414 |
+ |
|
415 |
+ |
SCAN * |
416 |
+ |
scanretire() /* retire old scanlines to free list */ |
417 |
+ |
{ |
418 |
+ |
SCAN *sold[NRETIRE]; |
419 |
+ |
int n; |
420 |
+ |
int h; |
421 |
+ |
register SCAN *sl; |
422 |
+ |
register int i; |
423 |
+ |
/* grab the NRETIRE oldest scanlines */ |
424 |
+ |
sold[n = 0] = NULL; |
425 |
+ |
for (h = 0; h < HSIZE; h++) |
426 |
+ |
for (sl = hashtab[h]; sl != NULL; sl = sl->next) { |
427 |
+ |
for (i = n; i && sold[i-1]->lused > sl->lused; i--) |
428 |
+ |
if (i < NRETIRE) |
429 |
+ |
sold[i] = sold[i-1]; |
430 |
+ |
if (i < NRETIRE) { |
431 |
+ |
sold[i] = sl; |
432 |
+ |
if (n < NRETIRE) /* grow list */ |
433 |
+ |
n++; |
434 |
+ |
} |
435 |
+ |
} |
436 |
+ |
/* put scanlines into free list */ |
437 |
+ |
for (i = 0; i < n; i++) { |
438 |
+ |
h = shash(sold[i]->y); |
439 |
+ |
sl = hashtab[h]; |
440 |
+ |
if (sl == sold[i]) |
441 |
+ |
hashtab[h] = sl->next; |
442 |
+ |
else { |
443 |
+ |
while (sl->next != sold[i]) /* IS in list */ |
444 |
+ |
sl = sl->next; |
445 |
+ |
sl->next = sold[i]->next; |
446 |
+ |
} |
447 |
+ |
if (i > 0) { /* save oldest as return value */ |
448 |
+ |
sold[i]->next = freelist; |
449 |
+ |
freelist = sold[i]; |
450 |
+ |
} |
451 |
+ |
} |
452 |
+ |
return(sold[0]); |
453 |
+ |
} |
454 |
+ |
|
455 |
+ |
|
456 |
+ |
static char *scan_buf; |
457 |
+ |
|
458 |
+ |
|
459 |
+ |
initscans() /* initialize scanline buffers */ |
460 |
+ |
{ |
461 |
+ |
int scansize; |
462 |
+ |
register SCAN *ptr; |
463 |
+ |
register int i; |
464 |
+ |
/* initialize positions */ |
465 |
+ |
scanpos = (long *)malloc(pysiz*sizeof(long)); |
466 |
+ |
if (scanpos == NULL) |
467 |
+ |
memerr("scanline positions"); |
468 |
+ |
for (i = pysiz-1; i >= 0; i--) |
469 |
+ |
scanpos[i] = -1L; |
470 |
+ |
curpos = pysiz-1; |
471 |
+ |
/* clear hash table */ |
472 |
+ |
for (i = 0; i < HSIZE; i++) |
473 |
+ |
hashtab[i] = NULL; |
474 |
+ |
/* allocate scanline buffers */ |
475 |
+ |
scansize = sizeof(SCAN) + pxsiz*sizeof(COLR); |
476 |
+ |
#ifdef ALIGN |
477 |
+ |
scansize = scansize+(sizeof(ALIGN)-1)) & ~(sizeof(ALIGN)-1); |
478 |
+ |
#endif |
479 |
+ |
i = MAXSBUF / scansize; /* compute number to allocate */ |
480 |
+ |
if (i > HSIZE) |
481 |
+ |
i = HSIZE; |
482 |
+ |
scan_buf = malloc(i*scansize); /* get in one big chunk */ |
483 |
+ |
if (scan_buf == NULL) |
484 |
+ |
memerr("scanline buffers"); |
485 |
+ |
ptr = (SCAN *)scan_buf; |
486 |
+ |
freelist = NULL; /* build our free list */ |
487 |
+ |
while (i-- > 0) { |
488 |
+ |
ptr->y = -1; |
489 |
+ |
ptr->lused = -1; |
490 |
+ |
ptr->next = freelist; |
491 |
+ |
freelist = ptr; |
492 |
+ |
ptr = (SCAN *)((char *)ptr + scansize); /* beware of C bugs */ |
493 |
+ |
} |
494 |
+ |
} |
495 |
+ |
|
496 |
+ |
|
497 |
+ |
donescans() /* free up scanlines */ |
498 |
+ |
{ |
499 |
+ |
free(scan_buf); |
500 |
+ |
free((char *)scanpos); |
501 |
|
} |