shithub: qoistream

Download patch

ref: 45418db47966d92700a285df680b6edfd0d1a43b
parent: 765e2086b63cf705bfddb85af870dec02ee71860
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Aug 9 11:46:14 EDT 2023

simplify imgread; free raw pic before writing compressed one

--- a/qoisend.c
+++ b/qoisend.c
@@ -88,11 +88,11 @@
 imgread(int in)
 {
 	char tmp[61], *f[5];
-	int r, n, e, w, h;
+	int e, w, h;
 	Img *i;
 
 	tmp[60] = 0;
-	if(pread(in, tmp, 60, 0) != 60 || tokenize(tmp, f, 5) != 5)
+	if(seek(in, 0, 0) != 0 || readn(in, tmp, 60) != 60 || tokenize(tmp, f, 5) != 5)
 		sysfatal("invalid image");
 	if(strcmp(f[0]+1, "8r8g8b8") != 0)
 		sysfatal("only [ax]8r8g8b8 is supported");
@@ -103,11 +103,9 @@
 	i = malloc(sizeof(*i) + e);
 	i->w = w;
 	i->h = h;
-	for(n = 0; n < e; n += r){
-		if((r = pread(in, i->bgrx+n, e-n, n+5*12)) <= 0){
-			free(i);
-			return nil;
-		}
+	if(readn(in, i->bgrx, e) != e){
+		free(i);
+		i = nil;
 	}
 
 	return i;
@@ -204,11 +202,11 @@
 		for(i = 0; i < 7; i++)
 			b[j++] = 0;
 		b[j++] = 1;
+		free(prev);
 
 		if(write(1, b, j) != j)
 			break;
 
-		free(prev);
 		prev = img;
 	}
 	sendul(done, 0);