]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/libmemdraw/write.c
merge
[plan9front.git] / sys / src / libmemdraw / write.c
index a8816ed040bb20acec468eeb791d5e4a9f09610b..b6819123c08f20bf1d8d8b0ce589f77f1eba2451 100644 (file)
@@ -27,12 +27,10 @@ writememimage(int fd, Memimage *i)
        uchar *line, *eline;                    /* input line, end pointer */
        uchar *data, *edata;                    /* input buffer, end pointer */
        ulong n;                                /* length of input buffer */
-       ulong nb;                               /* # of bytes returned by unloadimage */
        int bpl;                                /* input line length */
        int offs, runlen;                       /* offset, length of consumed data */
        uchar dumpbuf[NDUMP];                   /* dump accumulator */
        int ndump;                              /* length of dump accumulator */
-       int miny, dy;                           /* y values while unloading input */
        int ncblock;                            /* size of compressed blocks */
        Rectangle r;
        uchar *p, *q, *s, *es, *t;
@@ -41,28 +39,36 @@ writememimage(int fd, Memimage *i)
 
        r = i->r;
        bpl = bytesperline(r, i->depth);
+       ncblock = _compblocksize(r, i->depth);
+       if(ncblock > CHUNK){
+               sprint(hdr, "%11s %11d %11d %11d %11d ",
+                       chantostr(cbuf, i->chan), r.min.x, r.min.y, r.max.x, r.max.y);
+               if(write(fd, hdr, 5*12) != 5*12)
+                       return -1;
+               for(; r.min.y < r.max.y; r.min.y++)
+                       if(write(fd, byteaddr(i, r.min), bpl) != bpl)
+                               return -1;
+               return 0;
+       }
+
        n = Dy(r)*bpl;
        data = malloc(n);
-       ncblock = _compblocksize(r, i->depth);
+       if(data == 0){
+       ErrOut0:
+               free(data);
+               return -1;
+       }
+       if(unloadmemimage(i, r, data, n) != n)
+               goto ErrOut0;
        outbuf = malloc(ncblock);
        hash = malloc(NHASH*sizeof(Hlist));
        chain = malloc(NMEM*sizeof(Hlist));
-       if(data == 0 || outbuf == 0 || hash == 0 || chain == 0){
+       if(outbuf == 0 || hash == 0 || chain == 0){
        ErrOut:
-               free(data);
                free(outbuf);
                free(hash);
                free(chain);
-               return -1;
-       }
-       for(miny = r.min.y; miny != r.max.y; miny += dy){
-               dy = r.max.y-miny;
-               if(dy*bpl > CHUNK)
-                       dy = CHUNK/bpl;
-               nb = unloadmemimage(i, Rect(r.min.x, miny, r.max.x, miny+dy),
-                       data+(miny-r.min.y)*bpl, dy*bpl);
-               if(nb != dy*bpl)
-                       goto ErrOut;
+               goto ErrOut0;
        }
        sprint(hdr, "compressed\n%11s %11d %11d %11d %11d ",
                chantostr(cbuf, i->chan), r.min.x, r.min.y, r.max.x, r.max.y);