5 #define HSHIFT 3 /* HSHIFT==5 runs slightly faster, but hash table is 64x bigger */
6 #define NHASH (1<<(HSHIFT*NMATCH))
7 #define HMASK (NHASH-1)
8 #define hupdate(h, c) ((((h)<<HSHIFT)^(c))&HMASK)
9 typedef struct Hlist Hlist;
16 writeimage(int fd, Image *i, int dolock)
18 uchar *outbuf, *outp, *eout; /* encoded data, pointer, end */
19 uchar *loutp; /* start of encoded line */
20 Hlist *hash; /* heads of hash chains of past strings */
21 Hlist *chain, *hp; /* hash chain members, pointer */
22 Hlist *cp; /* next Hlist to fall out of window */
23 int h; /* hash value */
24 uchar *line, *eline; /* input line, end pointer */
25 uchar *data, *edata; /* input buffer, end pointer */
26 ulong n; /* length of input buffer */
27 ulong nb; /* # of bytes returned by unloadimage */
28 int bpl; /* input line length */
29 int offs, runlen; /* offset, length of consumed data */
30 uchar dumpbuf[NDUMP]; /* dump accumulator */
31 int ndump; /* length of dump accumulator */
32 int miny, dy; /* y values while unloading input */
35 uchar *p, *q, *s, *es, *t;
39 chunk = i->display->bufsize - 32; /* a little room for header */
41 bpl = bytesperline(r, i->depth);
49 for(miny = r.min.y; miny != r.max.y; miny += dy){
56 lockdisplay(i->display);
57 nb = unloadimage(i, Rect(r.min.x, miny, r.max.x, miny+dy),
58 data+(miny-r.min.y)*bpl, dy*bpl);
60 unlockdisplay(i->display);
64 ncblock = _compblocksize(r, i->depth);
66 sprint(hdr, "%11s %11d %11d %11d %11d ",
67 chantostr(cbuf, i->chan), r.min.x, r.min.y, r.max.x, r.max.y);
68 if(write(fd, hdr, 5*12) != 5*12)
70 if(write(fd, data, n) != n)
76 outbuf = malloc(ncblock);
77 hash = malloc(NHASH*sizeof(Hlist));
78 chain = malloc(NMEM*sizeof(Hlist));
79 if(outbuf == 0 || hash == 0 || chain == 0){
86 sprint(hdr, "compressed\n%11s %11d %11d %11d %11d ",
87 chantostr(cbuf, i->chan), r.min.x, r.min.y, r.max.x, r.max.y);
88 if(write(fd, hdr, 11+5*12) != 11+5*12)
91 eout = outbuf+ncblock;
95 memset(hash, 0, NHASH*sizeof(Hlist));
96 memset(chain, 0, NMEM*sizeof(Hlist));
100 for(n = 0; n != NMATCH; n++)
101 h = hupdate(h, line[n]);
103 while(line != edata){
106 for(p = line; p != eline; ){
113 for(hp = hash[h].next; hp; hp = hp->next){
137 if(eout-outp < ndump+1)
139 *outp++ = ndump-1+128;
140 memmove(outp, dumpbuf, ndump);
144 dumpbuf[ndump++] = *p;
149 if(eout-outp < ndump+1)
151 *outp++ = ndump-1+128;
152 memmove(outp, dumpbuf, ndump);
159 *outp++ = ((runlen-NMATCH)<<2) + (offs>>8);
162 for(q = p+runlen; p != q; p++){
165 cp->next = hash[h].next;
171 if(++cp == &chain[NMEM])
174 h = hupdate(h, p[NMATCH]);
178 if(eout-outp < ndump+1)
180 *outp++ = ndump-1+128;
181 memmove(outp, dumpbuf, ndump);
192 sprint(hdr, "%11d %11ld ", r.max.y, n);
193 write(fd, hdr, 2*12);
194 write(fd, outbuf, n);