shithub: blie

ref: 807afae69b7551b681b638d64f1099af5c672fdd
dir: /p9image.c/

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

Cursor ccircle = {
	{-7, -7},
	{0xFF, 0xFF, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x07,
	 0xe0, 0x07, 0xe0, 0x07, 0xe0, 0x07, 0xe0, 0x07,
	 0xe0, 0x07, 0xe0, 0x07, 0xe0, 0x07, 0xe0, 0x07,
	 0xe0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xFF, 0xFF},
	{0x00, 0x00, 0x7f, 0xfe, 0x40, 0x02, 0x40, 0x02,
	 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02,
	 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02,
	 0x40, 0x02, 0x40, 0x02, 0x7f, 0xfe, 0x00, 0x00}
};

Point toolcell;
#define NUMCELLS (5)

Image *tmpcol;

typedef struct Data Data;
struct Data {
	Memimage *img;
	Memimage *mask;
	Drawop op;
};

typedef enum {
	Composite,
	Img,
	Mask,
} Mode;

enum {
	DTimg = 0,
	DTmask,
};

typedef struct Brush Brush;
struct Brush {
	Memimage *i;
	int r;
};

typedef struct Tstate Tstate;
struct Tstate {
	Mode mode;
	int drawtarget;
	Image *circle;
	Memimage *circlebrush;
	Memimage *colorbrush;
	int brushrad;
	
	int cbrad;
	double cbexp;
	double cbmult;
	
	int curbrush;
	int curcolor;
	
	Brush brushes[NUMCELLS];
	ulong colordata[NUMCELLS];
};
Tstate tstate;

static Brush*
getcurrentbrush(void)
{
	if (tstate.curbrush < 4) /* three cells reserved */
		return &tstate.brushes[3];
	if (tstate.curbrush >= NUMCELLS)
		return nil;
	return &tstate.brushes[tstate.curbrush];
}

static void
setcolorbrush(ulong color)
{
	if (tstate.colorbrush)
		freememimage(tstate.colorbrush);
	tstate.colorbrush = allocmemimage(Rect(0, 0, 1, 1), RGB24);
	tstate.colorbrush->flags |= Frepl|Fsimple|Fbytes;
	memfillcolor(tstate.colorbrush, color);
}

static Memimage*
getcurrentcolor(void)
{
	if (tstate.curcolor < 0 || tstate.curcolor >= NUMCELLS)
		return nil;
	setcolorbrush(tstate.colordata[tstate.curcolor]);
	return tstate.colorbrush;
}

static double
distance(Point p, Point q)
{
	double n;
	p = subpt(q, p);
	p.x *= p.x;
	p.y *= p.y;
	n = sqrt((double)p.x + (double)p.y);
	return n;
}

static int
isaturate(int a)
{
	return a < 0 ? 0 : (a > 255 ? 255 : a);
}

static void
setcirclebrush(int r, double exp, double mult)
{
	int x, y, d, n;
	double dist;
	
	tstate.cbrad = r;
	tstate.cbexp = exp;
	tstate.cbmult = mult;
	
	d = r*2 + 1;
	if (tstate.circlebrush)
		freememimage(tstate.circlebrush);
	tstate.brushrad = r;
	tstate.circlebrush = allocmemimage(Rect(0, 0, d, d), GREY8);
	tstate.circlebrush->flags |= Fbytes|Falpha;
	for (y = 0; y < d; y++)
		for (x = 0; x < d; x++) {
			dist = distance(Pt(x, y), Pt(r, r))/r;
			if (dist > 1.) {
				*byteaddr(tstate.circlebrush, Pt(x, y)) = 0x0;
				continue;
			}
			dist = pow(1. - dist, exp) * mult;
			
			n = (int)(dist * 256.);
			*byteaddr(tstate.circlebrush, Pt(x, y)) = isaturate(n);
		}
	
	tstate.brushes[3].i = tstate.circlebrush;
	tstate.brushes[3].r = tstate.brushrad;
}

static void
readcolors(void)
{
	int fd, i;
	Biobuf *bin;
	char *s;
	char *args[2];
	ulong val;
	
	fd = open("p9image/colors", OREAD);
	if (fd < 0)
		return;
	
	bin = Bfdopen(fd, OREAD);
	if (!bin)
		sysfatal("%r");
	
	while (s = Brdstr(bin, '\n', 1)) {
		if (tokenize(s, args, 2) == 2) {
			i = atoi(args[0]);
			val = strtoul(args[1], nil, 16);
			tstate.colordata[i] = val;
		} else
			fprint(2, "ignoring line: %s\n", args[0]);
		free(s);
	}
}

static int
writecolors(void)
{
	int fd, i;
	ulong c;
	if (access("p9image", AEXIST)) {
		fd = create("p9image", OREAD, DMDIR|0555);
		if (fd < 0) {
			werrstr("p9image: %r");
			return 0;
		}
		close(fd);
	}
	fd = open("p9image/colors", OWRITE|OTRUNC);
	if (fd < 0)
		fd = create("p9image/colors", OWRITE|OTRUNC, 0666);
	if (fd < 0) {
		werrstr("p9image: colors: %r");
		return 0;
	}
	
	for (i = 0; i < NUMCELLS; i++) {
		c = tstate.colordata[i];
		fprint(fd, "%d\t%ulx\n", i, c&0xff ? c : 0);
	}
	
	close(fd);
	return 1;
}

static void
p9initialize()
{
	tstate.mode = Composite;
	tstate.drawtarget = DTimg;
	
	toolcell = Pt(15, vdata.fontheight + 4);
	
	if (headless)
		return;
	
	tstate.circle = allocimage(display, Rect(0, 0, 41, 41), RGBA32, 0, DTransparent);
	ellipse(tstate.circle, Pt(20, 20), 19, 19, 0, display->white, ZP);
	ellipse(tstate.circle, Pt(20, 20), 20, 20, 0, display->black, ZP);
	
	setcirclebrush(20, 1., 1.);
	setcolorbrush(DRed);
	tstate.curbrush = 3;
	tstate.curcolor = -1;
	
	/* load tools */
	readcolors();
}

static int
writelayer(Layer *l)
{
	Data *d;
	int fd;
	char *s;
	
	if (!l->data) {
		werrstr("p9image: layer not initialized: %s", l->name);
		return 0;
	}
	d = (Data*)l->data;
	
	/* image file */
	if (!d->img) {
		werrstr("p9image: no image");
		return 0;
	}
	
	s = smprint("l/%s/img", l->name);
	fd = open(s, OWRITE|OTRUNC);
	if (fd < 0)
		fd = create(s, OWRITE|OTRUNC, 0666);
	if (fd < 0) {
		werrstr("p9image: %r");
		free(s);
		return 0;
	}
	free(s);
	if (writememimage(fd, d->img)) {
		close(fd);
		werrstr("p9image: %r");
		return 0;
	}
	close(fd);
	
	/* mask file */
	if (!d->mask)
		return 1;
	
	s = smprint("l/%s/mask", l->name);
	fd = open(s, OWRITE|OTRUNC);
	if (fd < 0)
		fd = create(s, OWRITE|OTRUNC, 0666);
	if (fd < 0) {
		werrstr("p9image: %r");
		free(s);
		return 0;
	}
	free(s);
	if (writememimage(fd, d->mask)) {
		close(fd);
		werrstr("p9image: %r");
		return 0;
	}
	close(fd);
	return 1;
}

static void
p9init(Layer *l)
{
	int fd;
	char *s;
	Data *d;
	
	if (l->data)
		return;
	d = mallocz(sizeof(Data), 1);
	l->data = d;
	
	/* image file */
	s = smprint("l/%s/img", l->name);
	fd = open(s, OREAD);
	if (fd < 0) {
		free(s);
		return;
	}
	free(s);
	
	seek(fd, 0, 0);
	d->img = creadmemimage(fd);
	if (!d->img) {
		seek(fd, 0, 0);
		d->img = readmemimage(fd);
	}
	close(fd);
	
	/* mask file */
	s = smprint("l/%s/mask", l->name);
	fd = open(s, OREAD);
	if (fd < 0) {
		free(s);
		return;
	}
	free(s);
	
	seek(fd, 0, 0);
	d->mask = creadmemimage(fd);
	if (!d->mask) {
		seek(fd, 0, 0);
		d->mask = readmemimage(fd);
	}
	close(fd);
}

/* just use ecompose, which uses raw() and mask() */
static Memimage*
p9composite(Layer *l, Memimage *img)
{
	Data *d;
	d = (Data*)l->data;
	
	if (!img) {
		fprint(2, "%s: return input image: %p\n", l->name, d->img);
		return dupmemimage(d->img);
	}
	
	fprint(2, "%s: return composite image: %p %p %p\n", l->name,
		img, d->img, d->mask);
	return gencomposite(img, d->img, d->mask, l->op);
}

static Memimage*
p9raw(Layer *l)
{
	Data *d;
	d = (Data*)l->data;
	return d->img;
}

static Memimage*
p9mask(Layer *l)
{
	Data *d;
	d = (Data*)l->data;
	return d->mask;
}

static int
p9overlay(Layer *l, Image *i)
{
	Data *data;
	Memimage *mi;
	
	data = (Data*)l->data;
	
	if (!i)
		return tstate.mode != Composite;
	
	switch (tstate.mode) {
	case Composite:
		break;
	case Img:
		mi = data->img;
		goto Mout;
	case Mask:
		mi = data->mask;
		goto Mout;
	}
	changecursor(nil, nil, ZP);
	return 0;
Mout:
	changecursor(&ccircle, tstate.circle, Pt(-20, -20));
	if (!mi)
		return 0;
	
	setdrawingdirty(Dcontent);
	sampleview(i, mi, 0);
	return 0;
}

static Rectangle
p9toolrect(Layer *l)
{
	return Rect(0, 0, 200, 50);
}

static void
drcells(Image *i, Point p, char *s, int hl)
{
	Rectangle r;
	r.min = p;
	r.max = addpt(p, toolcell);
	border(i, r, 1, vdata.gray, ZP);
	if (hl) {
		r = insetrect(r, 2);
		draw(i, r, vdata.gray, nil, ZP);
	}
	string(i, addpt(p, Pt(2, 2)), display->black, ZP, font, s);
}

static void
drcols(Image *i, Point p, int n, int hl)
{
	Rectangle r;
	r.min = p;
	r.max = addpt(p, toolcell);
	tmpcol = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, tstate.colordata[n]);
	draw(i, r, tmpcol, nil, ZP);
	freeimage(tmpcol);
	border(i, r, 1, hl ? display->black : vdata.gray, ZP);
}

static void
drbrush(Image *i, Point p, int n, int hl)
{
	Rectangle r;
	r.min = p;
	r.max = addpt(p, toolcell);
	border(i, r, 1, hl ? display->black : vdata.gray, ZP);
}

static void
p9drawtools(Layer *l, Image *i)
{
	int n;
	Point p;
	p = i->r.min;
	draw(i, i->r, display->white, nil, ZP);
	
	drcells(i, p, "C", tstate.mode == Composite);
	p.x += toolcell.x;
	drcells(i, p, "S", tstate.mode == Img);
	p.x += toolcell.x;
	drcells(i, p, "M", tstate.mode == Mask);
	p.x += toolcell.x;
	
	for (n = 3; n < NUMCELLS; n++) {
		drbrush(i, p, n, tstate.curbrush == n);
		p.x += toolcell.x;
	}
	
	p.y += toolcell.y;
	p.x = i->r.min.x;
	for (n = 0; n < NUMCELLS; n++) {
		drcols(i, p, n, tstate.curcolor == n);
		p.x += toolcell.x;
	}
}

static int
p9savedata(Layer *l)
{
	return writelayer(l);
}

static int
p9savetools(Layer*)
{
	/* at the moment, this will write the data for each layer
	 * into the same file, which is not wrong, but could
	 * be improved.
	 */
	return writecolors();
}

static Redrawwin
drawbrush(Layer *l, int buttons, Point xy)
{
	Memimage *tgt;
	Data *d;
	Rectangle r;
	Brush *brush;
	Memimage *color;
	
	d = (Data*)l->data;
	
	if (!buttons)
		return Rnil;
	
	brush = getcurrentbrush();
	color = getcurrentcolor();
	
	if (!(brush && brush->i && color))
		return Rnil;
	
	tgt = nil;
	if (tstate.drawtarget == DTimg)
		tgt = d->img;
	else if (tstate.drawtarget == DTmask)
		tgt = d->mask;
	
	if (!tgt)
		return Rnil;
	
	r = insetrect(tgt->r, -brush->r);
	if (!ptinrect(xy, r))
		return Rnil;
	
	r = rectaddpt(brush->i->r,
		subpt(
			addpt(tgt->r.min, xy),
			Pt(brush->r, brush->r)
		)
	);
	
	color->clipr = brush->i->r;
	memimagedraw(tgt, r, color, ZP, brush->i, ZP, SoverD);
	setdrawingdirty(Dcontent);
	dirtylayer(l);
	return Rdrawing;
}

static Redrawwin
p9drawinput(Layer *l, int e, Event ev)
{
	switch (e) {
	case Ekeyboard:
		break;
	case Emouse:
		return drawbrush(l, ev.mouse.buttons, ev.mouse.xy);
		break;
	}
	return Rnil;
}

static void
selectbrush(int num)
{
}

static void
selectcolor(int num)
{
	if (num < 0 || num >= NUMCELLS)
		return;
	tstate.curcolor = num;
}

static void
setbrush(int num)
{
}

static void
setcolor(int num)
{
	ulong col;
	
	if (num < 0 || num >= NUMCELLS)
		return;
	
	if (!eentercolor("color", &col, &vstate.lastmouse))
		return;
	tstate.colordata[num] = col;
}

static void
configcirclebrush(Mouse *m)
{
	char buf[256];
	char *args[3];
	int r;
	double e, mu;
	
	snprint(buf, sizeof buf, "%d %f %f", tstate.cbrad, tstate.cbexp, tstate.cbmult);
	if (eenter("circle (rad, exp, mult)", buf, sizeof buf, m) < 0)
		return;
	
	if (tokenize(buf, args, 3) != 3)
		return;
	
	r = atoi(args[0]);
	e = atof(args[1]);
	mu = atof(args[2]);
	setcirclebrush(r, e, mu);
}

static Redrawwin
p9toolinput(Layer *l, int e, Event ev)
{
	Point xy;
	if (e != Emouse)
		return Rnil;
	
	if (!ev.mouse.buttons)
		return Rnil;
	
	if (ev.mouse.xy.y / toolcell.y == 0) {
		switch (ev.mouse.xy.x / toolcell.x) {
		case 0:
			tstate.mode = Composite;
			tstate.drawtarget = DTimg;
			goto Out;
		case 1:
			tstate.mode = Img;
			tstate.drawtarget = DTimg;
			goto Out;
		case 2:
			tstate.mode = Mask;
			tstate.drawtarget = DTmask;
			goto Out;
		}
	}
	xy.x = ev.mouse.xy.x / toolcell.x;
	xy.y = ev.mouse.xy.y / toolcell.y;
	
	if (ev.mouse.buttons & 1) {
		/* left mouse button */
		switch (xy.y) {
		case 0:
			if (xy.x < 3)
				return Rnil;
			selectbrush(xy.x);
			return Rtools;
		case 1:
			selectcolor(xy.x);
			return Rtools;
		}
		return Rnil;
	}
	if (ev.mouse.buttons & 4) {
		/* right mouse button */
		switch (xy.y) {
		case 0:
			if (xy.x < 3)
				return Rnil;
			if (xy.x == 3) {
				/* special case for circle brush */
				configcirclebrush(&ev.mouse);
				return Rnil;
			}
			setbrush(xy.x);
			return Rtools;
		case 1:
			setcolor(xy.x);
			return Rtools;
		}
		return Rnil;
	}
	return Rnil;
Out:
	setdrawingdirty(Dcontent);
	return Rdrawing|Rtools;
}

Editor p9image = {
	.name = "p9img",
	.init = p9initialize,
	.initlayer = p9init,
	.raw = p9raw,
	.mask = p9mask,
	.overlay = p9overlay,
	.toolrect = p9toolrect,
	.drawtools = p9drawtools,
	.savedata = p9savedata,
	.savetools = p9savetools,
	.drawinput = p9drawinput,
	.toolinput = p9toolinput,
};