shithub: ifilter

Download patch

ref: 00f9770a0cac05bb7b03e1b3b80f76743dac8639
parent: d41ee93f06b95c540cb19388795766aded1482c0
author: phil9 <telephil9@gmail.com>
date: Sat Nov 27 04:01:12 EST 2021

add a simple pixelation filter

--- a/README
+++ b/README
@@ -24,6 +24,10 @@
 -----
 blur implements blurring algorithms.
 
+Usage: blur [-s size] [box|pixelate]
+
 Available filters are:
 - box: 3x3 box blur
+- pixelate: a simple pixelation filter using `-s` argument as pixel size
+
 
--- a/blur.c
+++ b/blur.c
@@ -4,6 +4,7 @@
 #include <memdraw.h>
 
 typedef struct Filter Filter;
+int size;
 
 struct Filter
 {
@@ -11,8 +12,6 @@
 	uchar* (*filter)(uchar *data, int w, int h, int depth);
 };
 
-enum { R, G, B };
-
 uchar*
 box(uchar *data, int w, int h, int depth)
 {
@@ -43,15 +42,43 @@
 	return out;
 }
 
+uchar*
+pixelate(uchar *data, int w, int h, int depth)
+{
+	uchar *out;
+	int x, y, i, ox, oy, oi;
+
+	if(size < 0)
+		sysfatal("pixelate filter needs a size argument");
+	out = malloc(depth*w*h*sizeof(uchar));
+	if(out == nil)
+		return nil;
+	for(y = 0; y < h; y += size){
+		for(x = 0; x < w; x += size){
+			i = (x + w * y) * depth;
+			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];
+				}
+			}
+		}
+	}
+	return out;
+}
+
 void
 usage(void)
 {
-	fprint(2, "usage: %s [box]\n", argv0);
+	fprint(2, "usage: %s [-s size] [box|pixelate]\n", argv0);
 	exits("usage");
 }
 
 static Filter filters[] = {
 	{ "box", box },
+	{ "pixelate", pixelate },
 };
 
 void
@@ -62,8 +89,12 @@
 	uchar *buf, *out;
 	Filter *f;
 
+	size = -1;
 	f = nil;
 	ARGBEGIN{
+	case 's':
+		size = atoi(EARGF(usage()));
+		break;
 	default:
 		usage();
 		break;