|
|
1.1 root 1: /*
2: * Type-specific code for TYPE=runcode
3: */
4: #include <picfile.h>
5: #include <libc.h>
6: #define NBUF 4096 /* size of input buffer */
7: int PRD_run(f, buf)
8: PICFILE *f;
9: char *buf;
10: {
11: register char *bufp, *runp, *erunp;
12: int i, j, n, slack;
13: if(f->line==0){
14: f->buf=malloc(NBUF);
15: if(f->buf==0){
16: PIC_error="Can't allocate buffer";
17: return 0;
18: }
19: f->ebuf=f->bufp=f->buf;
20: }
21: if(f->line==f->height){
22: PIC_error="Read past end of picture";
23: return 0;
24: }
25: bufp=buf;
26: for(i=0;i!=f->width;){
27: while(f->ebuf-f->bufp<f->nchan+1){ /* read until buffer has complete runs */
28: slack=f->ebuf-f->bufp;
29: runp=f->buf;
30: while(f->bufp!=f->ebuf)
31: *runp++ = *f->bufp++;
32: n=read(f->fd, runp, NBUF-slack);
33: if(n<=0){
34: if(n==0) PIC_error="EOF reading picture";
35: return 0;
36: }
37: f->ebuf=runp+n;
38: f->bufp=f->buf;
39: }
40: n=*f->bufp++&255;
41: i+=n+1;
42: if(i>f->width){
43: PIC_error="run spans scan lines";
44: return 0;
45: }
46: runp=bufp;
47: for(j=0;j!=f->nchan;j++)
48: *bufp++=*f->bufp++;
49: erunp=bufp+n*f->nchan;
50: while(bufp!=erunp)
51: *bufp++ = *runp++;
52: }
53: f->line++;
54: return 1;
55: }
56: int PWR_run(f, buf)
57: PICFILE *f;
58: char *buf;
59: {
60: register char *last, *runp, *pix, *next;
61: int nrun, nchan, i;
62: if(f->line==0){
63: f->buf=malloc(f->width*(f->nchan+1)); /* big enough for any scan line */
64: if(f->buf==0){
65: PIC_error="Can't allocate buffer";
66: return 0;
67: }
68: PWR_header(f);
69: }
70: if(f->line>=f->height){
71: PIC_error="Write past end of picture";
72: return 0;
73: }
74: f->line++;
75: nchan=f->nchan;
76: last=buf+nchan*(f->width-1);
77: runp=f->buf;
78: while(buf<=last){
79: pix=buf;
80: next=buf+nchan;
81: while(next<=last && *pix==*next++)
82: pix++;
83: nrun=(pix-buf)/nchan;
84: pix=buf;
85: buf+=nrun*nchan+nchan;
86: while(nrun>=255){
87: *runp++=255;
88: i=nchan;
89: do
90: *runp++=*pix++;
91: while(--i>0);
92: pix-=nchan;
93: nrun-=256;
94: }
95: if(nrun>=0){
96: *runp++=nrun;
97: i=nchan;
98: do
99: *runp++=*pix++;
100: while(--i>0);
101: }
102: }
103: return write(f->fd, f->buf, runp-f->buf)==runp-f->buf;
104: }
105: int PCL_run(f)
106: PICFILE *f;
107: {
108: free(f->buf);
109: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.