shithub: blie

ref: 0e8796746fad950ab61be075de5638fdcbd6fc32
dir: /p9image.c/

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

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

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("%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("%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;
	
	p9init(l);
	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;
	p9init(l);
	d = (Data*)l->data;
	return d->img;
}

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

static Memimage*
p9overlay(Layer *l)
{
	p9init(l);
	return nil;
}

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

static void
p9input(Layer *l, Event)
{
	p9init(l);
}

Editor p9image = {
	.name = "p9img",
	.raw = p9raw,
	.mask = p9mask,
	.overlay = p9overlay,
	.savedata = p9savedata,
	.input = p9input
};