shithub: blie

ref: 72ea71f4d629f81f7c5eb34d31cf4b873162d06b
dir: /util.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include <event.h>
#include <keyboard.h>
#include "blie.h"

#define RGB2K(r,g,b)	((156763*(r)+307758*(g)+59769*(b))>>19)

uchar
rgb2k(uchar r, uchar g, uchar b)
{
	return RGB2K(r, g, b);
}

uchar
ilerp(uchar A, uchar B, uchar α)
{
	return flerp((double)A / 255., (double)B / 255., (double)α / 255.) * 255;
}

uchar
imul(uchar A, uchar B)
{
	return (((double)A * (double)B) / (255. * 255.)) * 255;
}

double
flerp(double A, double B, double α)
{
	return (B * α) + (1.-α) * A;
}

Memimage*
dupmemimage(Memimage *i)
{
	Memimage *d;
	d = gencanvas(i);
	
	memimagedraw(d, i->r, i, ZP, nil, ZP, SoverD);
	return d;
}

Memimage*
gencanvas(Memimage *i)
{
	i = allocmemimage(i->r, RGBA32);
	memfillcolor(i, DTransparent);
	return i;
}

Memimage*
gencomposite(Memimage *A, Memimage *B, Memimage *M, Drawop op)
{
	Memimage *d;
	
	d = dupmemimage(A);
	memimagedraw(d, B->r, B, ZP, M, ZP, op);
	return d;
}

Memimage*
ecomposite(Layer *l, Memimage *img)
{
	Memimage *s;
	Memimage *m;
	
	s = l->editor->raw(l);
	m = l->editor->mask(l);
	
	if (!img) {
		if (bliedebug > 1)
			fprint(2, "  c: raw image\n");
		return dupmemimage(s);
	}
	if (bliedebug > 1)
		fprint(2, "  c: combine images\n");
	return gencomposite(img, s, m, l->op);
}

Point
scalepos(Point xy)
{
	xy = addpt(xy, vstate.offset);
	xy.x = xy.x / vstate.zoom;
	xy.y = xy.y / vstate.zoom;
	return xy;
}