shithub: util

ref: 5d9892c1ed5a089b17181030616f8e49a333848d
dir: /edges.c/

View raw version
/*
 * find and draw edges in images to produce coloring pages
 * ex:
 *     png -t < image.png | edges | topng > imageedges.png
 */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>

void main() {
	Memimage *i;
	Memimage *o;
	int x, y, c;
	int nchan;
	int width;
	int height;

	i = readmemimage(0);
	o = allocmemimage(i->r, strtochan("k8"));
	memfillcolor(o, DBlack);
	nchan = i->nchan;
	width = Dx(i->r);
	height = Dy(i->r);
	for (y = 1; y < (height-1); y++)
		for (x = 1; x < (width-1); x++)
			for (c = 0; c < nchan; c++) {
				*(byteaddr(o, Pt(x, y))) += (255 - (
									  (*(byteaddr(i, Pt(x-1, y-1))+c))*(-1)
									+ (*(byteaddr(i, Pt(x-1, y+0))+c))*(-2)
									+ (*(byteaddr(i, Pt(x-1, y+1))+c))*(-1)
									+ (*(byteaddr(i, Pt(x+1, y-1))+c))*(1)
									+ (*(byteaddr(i, Pt(x+1, y+0))+c))*(2)
									+ (*(byteaddr(i, Pt(x+1, y+1))+c))*(1)
									+ (*(byteaddr(i, Pt(x-1, y-1))+c))*(-1)
									+ (*(byteaddr(i, Pt(x+0, y-1))+c))*(-2)
									+ (*(byteaddr(i, Pt(x+1, y-1))+c))*(-1)
									+ (*(byteaddr(i, Pt(x-1, y+1))+c))*(1)
									+ (*(byteaddr(i, Pt(x+0, y+1))+c))*(2)
									+ (*(byteaddr(i, Pt(x+1, y+1))+c))*(1)
									))/nchan;
			}
	freememimage(i);
	writememimage(1, o);
	freememimage(o);
}