shithub: ifilter

Download patch

ref: bf1934aa4a1bbc6b13d8008c4a044a0d66dbadb7
parent: 13898397ae1c6cf50f0e600a265f61ac5c423d11
author: phil9 <telephil9@gmail.com>
date: Thu Dec 2 00:12:22 EST 2021

cfilter: properly handle alpha images

	images were transformed to XRGB32 whatsoever instead of
	checking if the input had an alpha channel. This lead to
	wrong colors in the output image.
	We now check the input image chan and transform to XRGB32 or
	ARGB32 based on that.

--- a/cfilter.c
+++ b/cfilter.c
@@ -72,11 +72,22 @@
 	{ "tint", tint },
 };
 
+int
+isalpha(ulong chan)
+{
+	ulong t;
+
+	for(t = chan; t; t >>= 8)
+		if(TYPE(t) == CAlpha)
+			return 1;
+	return 0;
+}
+
 void
 main(int argc, char *argv[])
 {
 	Memimage *o, *i;
-	int w, h, p, n;
+	int w, h, p, n, a;
 	uchar *buf;
 	Filter *f;
 	float factor;
@@ -110,7 +121,8 @@
 	o = readmemimage(0);
 	if(o==nil)
 		sysfatal("readmemimage: %r");
-	i = allocmemimage(o->r, XRGB32);
+	a = isalpha(o->chan);
+	i = allocmemimage(o->r, a ? ARGB32 : XRGB32);
 	memimagedraw(i, i->r, o, o->r.min, nil, ZP, S);
 	freememimage(o);
 	w = Dx(i->r);
@@ -124,7 +136,7 @@
 	freememimage(i);
 	for(p = 0; p < n; p+=4)
 		f->filter(buf+p, factor);
-	print("   x8r8g8b8 %11d %11d %11d %11d ", 0, 0, w, h);
+	print("   %c8r8g8b8 %11d %11d %11d %11d ", a ? 'a' : 'x', 0, 0, w, h);
 	write(1, buf, n);
 	exits(nil);
 }