ref: da5ef81858616aa4cd9bd350b2de30a8553330a3
dir: /p9image.c/
#include <u.h> #include <libc.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} }; typedef struct Data Data; struct Data { Memimage *img; Memimage *mask; Drawop op; }; typedef enum { Composite, Img, Mask, } Mode; typedef struct Tstate Tstate; struct Tstate { Mode mode; Image *circle; }; Tstate tstate; 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 void p9overlay(Layer *l, Image *i) { Data *data; Memimage *mi; p9init(l); data = (Data*)l->data; 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; Mout: if (!tstate.circle) { 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); } changecursor(&ccircle, tstate.circle, Pt(-20, -20)); if (!mi) return; sampleview(i, mi); } static Rectangle p9toolrect(Layer *l) { return Rect(0, 0, 200, 50); } static void p9drawtools(Layer *l, Image *i) { draw(i, insetrect(i->r, 5), display->white, nil, ZP); } static int p9savedata(Layer *l) { p9init(l); return 1; } static int p9drawinput(Layer *l, int e, Event) { p9init(l); switch (e) { case Ekeyboard: break; case Emouse: break; } return 0; } static int p9toolinput(Layer *l, int e, Event ev) { p9init(l); if (e != Emouse) return 0; if (!ev.mouse.buttons) return 0; switch (ev.mouse.xy.x / 50) { case 0: tstate.mode = Composite; break; case 1: tstate.mode = Img; break; case 2: tstate.mode = Mask; break; default: return 0; } return 1; } Editor p9image = { .name = "p9img", .raw = p9raw, .mask = p9mask, .overlay = p9overlay, .toolrect = p9toolrect, .drawtools = p9drawtools, .savedata = p9savedata, .drawinput = p9drawinput, .toolinput = p9toolinput, };