6 creadimage(Display *d, int fd, int dolock)
10 int m, nb, miny, maxy, new, ldepth, ncblock;
15 if(readn(fd, hdr, 5*12) != 5*12)
19 * distinguish new channel descriptor from old ldepth.
20 * channel descriptors have letters as well as numbers,
21 * while ldepths are a single digit formatted as %-11d.
31 werrstr("creadimage: bad format");
36 if((chan = strtochan(hdr)) == 0){
37 werrstr("creadimage: bad channel string %s", hdr);
41 ldepth = ((int)hdr[10])-'0';
42 if(ldepth<0 || ldepth>3){
43 werrstr("creadimage: bad ldepth %d", ldepth);
46 chan = drawld2chan[ldepth];
48 r.min.x=atoi(hdr+1*12);
49 r.min.y=atoi(hdr+2*12);
50 r.max.x=atoi(hdr+3*12);
51 r.max.y=atoi(hdr+4*12);
52 if(r.min.x>r.max.x || r.min.y>r.max.y){
53 werrstr("creadimage: bad rectangle");
60 i = allocimage(d, r, chan, 0, 0);
66 i = mallocz(sizeof(Image), 1);
70 setmalloctag(i, getcallerpc(&d));
71 ncblock = _compblocksize(r, chantodepth(chan));
72 buf = malloc(ncblock);
76 while(miny != r.max.y){
77 if(readn(fd, hdr, 2*12) != 2*12){
88 maxy = atoi(hdr+0*12);
90 if(maxy<=miny || r.max.y<maxy){
91 werrstr("creadimage: bad maxy %d", maxy);
94 if(nb<=0 || ncblock<nb){
95 werrstr("creadimage: bad count %d", nb);
98 if(readn(fd, buf, nb)!=nb)
103 a = bufimage(i->display, 21+nb);
108 BPLONG(a+5, r.min.x);
110 BPLONG(a+13, r.max.x);
112 if(!new) /* old image: flip the data bits */
113 _twiddlecompressed(buf, nb);
114 memmove(a+21, buf, nb);