shithub: riscv

Download patch

ref: f6e73a6a22db00925f2f447815287bf2a086054b
parent: 4749fc5ca13fd0dd568f5dbccc66c2fa1acd69dd
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Sun Sep 4 00:38:08 EDT 2011

libdraw: reduce memory for writeimage/writememimage

--- a/sys/src/libdraw/writeimage.c
+++ b/sys/src/libdraw/writeimage.c
@@ -39,6 +39,28 @@
 	chunk = i->display->bufsize - 32;	/* a little room for header */
 	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;
+		data = malloc(bpl);
+		for(miny = r.min.y; miny != r.max.y; miny++){
+			if(dolock)
+				lockdisplay(i->display);
+			nb = unloadimage(i, Rect(r.min.x, miny, r.max.x, miny+1), data, bpl);
+			if(dolock)
+				unlockdisplay(i->display);
+			if(nb != bpl)
+				goto ErrOut0;
+			if(write(fd, data, nb) != nb)
+				goto ErrOut0;
+		}
+		free(data);
+		return 0;
+	}
+
 	n = Dy(r)*bpl;
 	data = malloc(n);
 	if(data == 0){
@@ -60,17 +82,6 @@
 			unlockdisplay(i->display);
 		if(nb != dy*bpl)
 			goto ErrOut0;
-	}
-	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)
-			goto ErrOut0;
-		if(write(fd, data, n) != n)
-			goto ErrOut0;
-		free(data);
-		return 0;
 	}
 
 	outbuf = malloc(ncblock);
--- a/sys/src/libmemdraw/write.c
+++ b/sys/src/libmemdraw/write.c
@@ -27,12 +27,10 @@
 	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,38 +39,27 @@
 
 	r = i->r;
 	bpl = bytesperline(r, i->depth);
-	n = Dy(r)*bpl;
-	data = malloc(n);
-	if(data == 0){
-	ErrOut0:
-		free(data);
-		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;
-		if(dy <= 0)
-			dy = 1;
-		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 ErrOut0;
-	}
-
 	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)
-			goto ErrOut0;
-		if(write(fd, data, n) != n)
-			goto ErrOut0;
-		free(data);
+			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);
+	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));