~amavect/makeu

makeu/util.c -rw-r--r-- 797 bytes
8a2a5c28amavect revise error exiting, allocation errors 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Amavect! */
/* misc functions */
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include "fns.h"

/* clips the point to be the nearest point inside of r (excluding max) */
Point
ptclip(Point p, Rectangle r)
{
	if(p.x > r.max.x - 1)
		p.x = r.max.x - 1;
	else if(p.x < r.min.x)
		p.x = r.min.x;
	if(p.y > r.max.y - 1)
		p.y = r.max.y - 1;
	else if(p.y < r.min.y)
		p.y = r.min.y;
	return p;
}

void
error(char *s)
{
	fprint(2, "%s: %s: %r\n", argv0, s);
	threadexitsall(s);
}

Image *
eallocimage(Rectangle r, ulong chan, int repl, ulong col)
{
	Image *i;
	i = allocimage(display, r, chan, repl, col);
	if(i == nil)
		error("cannot allocimage");
	return i;
}

void *
emalloc(ulong size)
{
	void *p;
	p = malloc(size);
	if(p == nil)
		error("cannot malloc");
	return p;
}