shithub: ifilter

Download patch

ref: 0e8a14266851df16bcdf4ef9727dd22dc052e310
parent: 00f9770a0cac05bb7b03e1b3b80f76743dac8639
author: phil9 <telephil9@gmail.com>
date: Sat Nov 27 09:20:42 EST 2021

improved pixelate filter

	use an average of surrounding pixels colors instead of the single
	pixel at (x;y)

--- a/blur.c
+++ b/blur.c
@@ -46,7 +46,7 @@
 pixelate(uchar *data, int w, int h, int depth)
 {
 	uchar *out;
-	int x, y, i, ox, oy, oi;
+	int x, y, i, ox, oy, oi, sr, sg, sb;
 
 	if(size < 0)
 		sysfatal("pixelate filter needs a size argument");
@@ -55,13 +55,22 @@
 		return nil;
 	for(y = 0; y < h; y += size){
 		for(x = 0; x < w; x += size){
-			i = (x + w * y) * depth;
+			//i = (x + w * y) * depth;
+			sr = sg = sb = 0;
 			for(oy = y; oy < y+size; oy++){
 				for(ox = x; ox < x+size; ox++){
 					oi = (ox + w * oy) * depth;
-					out[oi + 0] = data[i + 0];
-					out[oi + 1] = data[i + 1];
-					out[oi + 2] = data[i + 2];
+					sr += data[oi + 0];
+					sg += data[oi + 1];
+					sb += data[oi + 2];
+				}
+			}
+			for(oy = y; oy < y+size; oy++){
+				for(ox = x; ox < x+size; ox++){
+					oi = (ox + w * oy) * depth;
+					out[oi + 0] = sr / (size*size);
+					out[oi + 1] = sg / (size*size);
+					out[oi + 2] = sb / (size*size);
 				}
 			}
 		}