11 int m, nb, miny, maxy, new, ldepth, ncblock;
16 if(readn(fd, hdr, 5*12) != 5*12){
17 werrstr("readmemimage: short header (2)");
22 * distinguish new channel descriptor from old ldepth.
23 * channel descriptors have letters as well as numbers,
24 * while ldepths are a single digit formatted as %-11d.
34 werrstr("creadimage: bad format");
39 if((chan = strtochan(hdr)) == 0){
40 werrstr("creadimage: bad channel string %s", hdr);
44 ldepth = ((int)hdr[10])-'0';
45 if(ldepth<0 || ldepth>3){
46 werrstr("creadimage: bad ldepth %d", ldepth);
49 chan = drawld2chan[ldepth];
51 r.min.x=atoi(hdr+1*12);
52 r.min.y=atoi(hdr+2*12);
53 r.max.x=atoi(hdr+3*12);
54 r.max.y=atoi(hdr+4*12);
55 if(r.min.x>r.max.x || r.min.y>r.max.y){
56 werrstr("creadimage: bad rectangle");
60 i = allocmemimage(r, chan);
63 ncblock = _compblocksize(r, i->depth);
64 buf = malloc(ncblock);
68 while(miny != r.max.y){
69 if(readn(fd, hdr, 2*12) != 2*12){
71 werrstr("readmemimage: short read");
77 maxy = atoi(hdr+0*12);
79 if(maxy<=miny || r.max.y<maxy){
80 werrstr("readimage: bad maxy %d", maxy);
83 if(nb<=0 || ncblock<nb){
84 werrstr("readimage: bad count %d", nb);
87 if(readn(fd, buf, nb)!=nb)
89 if(!new) /* old image: flip the data bits */
90 _twiddlecompressed(buf, nb);
91 cloadmemimage(i, Rect(r.min.x, miny, r.max.x, maxy), buf, nb);