shithub: subpixelize

ref: c575d9489e3157ff1fe5e85f0c91f920c0e75ab6
dir: /subpixelize.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>

static void
transformpx(Memimage *src, Memimage *dst, Point p)
{
	uchar *t;
	uchar *s1, *s2, *s3;
	uchar *s4, *s5, *s6;
	uchar *s7, *s8, *s9;
	
	t = byteaddr(dst, p);
	
	p.x *= 3;
	p.y *= 3;
	
	s1 = byteaddr(src, Pt(p.x  , p.y  ));
	s2 = byteaddr(src, Pt(p.x+1, p.y  ));
	s3 = byteaddr(src, Pt(p.x+2, p.y  ));
	s4 = byteaddr(src, Pt(p.x  , p.y+1));
	s5 = byteaddr(src, Pt(p.x+1, p.y+1));
	s6 = byteaddr(src, Pt(p.x+2, p.y+1));
	s7 = byteaddr(src, Pt(p.x  , p.y+2));
	s8 = byteaddr(src, Pt(p.x+1, p.y+2));
	s9 = byteaddr(src, Pt(p.x+2, p.y+2));
	
	t[2] = (*s1 + *s4 + *s7) / 3;
	t[1] = (*s2 + *s5 + *s8) / 3;
	t[0] = (*s3 + *s6 + *s9) / 3;
}

static void
transform(Memimage *src, Memimage *dst)
{
	int x, y;
	
	for (y = dst->r.min.y; y < dst->r.max.y; y++)
		for (x = dst->r.min.x; x < dst->r.max.x; x++)
			transformpx(src, dst, Pt(x, y));
}

void
main(void)
{
	Memimage *in, *out;
	Rectangle r;
	
	if (memimageinit() < 0)
		sysfatal("%r");
	
	in = readmemimage(0);
	if (!in)
		sysfatal("%r");
	
	r.min.x = in->r.min.x/3;
	r.min.y = in->r.min.y/3;
	r.max.x = r.min.x + Dx(in->r)/3;
	r.max.y = r.min.y + Dy(in->r)/3;
	
	out = allocmemimage(r, RGB24);
	if (!out)
		sysfatal("%r");
	
	transform(in, out);
	
	writememimage(1, out);
	exits(nil);
}