#include <9pm/u.h> #include <9pm/libc.h> #include <9pm/draw.h> /* * compressed data are sequences of byte codes. * if the first byte b has the 0x80 bit set, the next (b^0x80)+1 bytes * are data. otherwise, it's two bytes specifying a previous string to repeat. */ static void twiddlecompressed(uchar *buf, int n) { uchar *ebuf; int j, k, c; ebuf = buf+n; while(buf < ebuf){ c = *buf++; if(c >= 128){ k = c-128+1; for(j=0; j3){ werrstr("creadimage: bad ldepth %d", ldepth); return nil; } chan = drawld2chan[ldepth]; } r.min.x=atoi(hdr+1*12); r.min.y=atoi(hdr+2*12); r.max.x=atoi(hdr+3*12); r.max.y=atoi(hdr+4*12); if(r.min.x>r.max.x || r.min.y>r.max.y){ werrstr("creadimage: bad rectangle"); return nil; } if(dolock) lockdisplay(d); i = allocimage(d, r, chan, 0, 0); if(dolock) unlockdisplay(d); if(i == nil) return nil; buf = malloc(NCBLOCK); if(buf == nil) goto Errout; miny = r.min.y; while(miny != r.max.y){ if(readn(fd, hdr, 2*12) != 2*12){ Errout: if(dolock) lockdisplay(d); Erroutlock: freeimage(i); if(dolock) unlockdisplay(d); free(buf); return nil; } maxy = atoi(hdr+0*12); nb = atoi(hdr+1*12); if(maxy<=miny || r.max.yidisplay, 21+nb); if(a == nil) goto Erroutlock; a[0] = 'Y'; BPLONG(a+1, i->id); BPLONG(a+5, r.min.x); BPLONG(a+9, miny); BPLONG(a+13, r.max.x); BPLONG(a+17, maxy); if(!new) /* old image: flip the data bits */ twiddlecompressed(buf, nb); memmove(a+21, buf, nb); if(dolock) unlockdisplay(d); miny = maxy; } free(buf); return i; }