#include "paint.h" void * emalloc(ulong n) { void *p; if((p = malloc(n)) == nil) sysfatal("no memory"); setmalloctag(p, getcallerpc(&n)); return p; } int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } float fmax(float a, float b) { return a > b ? a : b; } int clampint(int min_, int x, int max_) { return max(min_, min(x, max_ - 1)); } Point clamppt(Point p, Rectangle b) { p.x = clampint(b.min.x, p.x, b.max.x); p.y = clampint(b.min.y, p.y, b.max.y); return p; } Rectangle clamprect(Rectangle r, Rectangle b) { rectclip(&r, b); return r; } Rectangle insetrectpt(Rectangle r, Point p) { r.min.x += p.x; r.min.y += p.y; r.max.x -= p.x; r.max.y -= p.y; return r; } Point rectdim(Rectangle r) { return Pt(Dx(r), Dy(r)); } Rectangle scalerect(Rectangle r, float s) { return Rect(r.min.x * s, r.min.y * s, r.max.x * s, r.max.y * s); } int adddim(int a, int b) { return a == INFDIM.x || b == INFDIM.x ? INFDIM.x : a + b; } Rdims adddims(Rdims a, Rdims b) { a.min = addpt(a.min, b.min); a.des = addpt(a.des, b.des); a.max.x = adddim(a.max.x, b.max.x); a.max.y = adddim(a.max.y, b.max.y); return a; } Point mtoipt(Point m, Widget *w) { Canvas *c; c = w->aux; return mulpt(m, c->mag); } Point mtospt(Point m, Widget *w) { Canvas *c; c = w->aux; return addpt(mtoipt(m, w), subpt(w->r.min, c->pos)); } Point stompt(Point s, Widget *w) { Canvas *c; c = w->aux; return divpt(addpt(subpt(s, w->r.min), c->pos), c->mag); } Point stoipt(Point s, Widget *w) { Canvas *c; c = w->aux; return addpt(subpt(s, w->r.min), c->pos); } Rectangle mtosrect(Rectangle m, Widget *w) { Canvas *c; c = w->aux; return rectaddpt(scalerect(m, c->mag), subpt(w->r.min, c->pos)); } Rectangle stomrect(Rectangle s, Widget *w) { Canvas *c; c = w->aux; return scalerect(rectsubpt(s, subpt(w->r.min, c->pos)), 1.0/c->mag); } Rectangle stoirect(Rectangle s, Widget *w) { return Rpt(stoipt(s.min, w), stoipt(s.max, w)); } Image * loadbitmap(Bitmap b) { Rectangle r; int d; Image *img; r = Rect(0, 0, b.Δx, b.Δy); if((d = chantodepth(b.chan)) == 0) { werrstr("bad chan"); return nil; } if((img = allocimage(display, Rect(0, 0, b.Δx, b.Δy), b.chan, 1, DNofill)) == nil) return nil; if(loadimage(img, img->r, b.data, b.Δy * bytesperline(r, d)) < 0) { freeimage(img); return nil; } return img; } void fastsetcursor(Mousectl *mctl, Cursor *c1) { static Cursor *c0; if(c1 != c0) setcursor(mctl, c0 = c1); } int atoifile(char *name, int *i) { int fd, n; char buf[16]; if((fd = open(name, OREAD)) < 0) return -1; if((n = read(fd, buf, sizeof(buf) - 1)) <= 0) { close(fd); return -1; } close(fd); buf[n] = '\0'; *i = atoi(buf); return 0; }