-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
66 lines (59 loc) · 1.07 KB
/
common.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "a.h"
int
isalpha(ulong chan)
{
ulong t;
for(t = chan; t; t >>= 8)
if(TYPE(t) == CAlpha)
return 1;
return 0;
}
void
applyfilter(const char *fname, filterfn f)
{
Memimage *o, *i;
int w, h, n, a;
uchar *buf, *out;
if(memimageinit()<0)
sysfatal("memimageinit: %r");
o = readmemimage(0);
if(o==nil)
sysfatal("readmemimage: %r");
a = isalpha(o->chan);
i = allocmemimage(rectsubpt(o->r, o->r.min), a ? ARGB32 : XRGB32);
memimagedraw(i, i->r, o, o->r.min, nil, ZP, S);
freememimage(o);
w = Dx(i->r);
h = Dy(i->r);
n = 4*w*h*sizeof(uchar);
buf = malloc(n);
if(buf==nil)
sysfatal("malloc: %r");
if(unloadmemimage(i, i->r, buf, n)<0)
sysfatal("unloadmemimage: %r");
freememimage(i);
out = f(buf, w, h, 4);
if(out == nil)
sysfatal("%s failed: %r", fname);
print(" %c8r8g8b8 %11d %11d %11d %11d ", a ? 'a' : 'x', 0, 0, w, h);
write(1, out, n);
exits(nil);
}
int
clamp(int n)
{
if(n < 0)
n = 0;
else if(n > 255)
n = 255;
return n;
}
uchar*
eallocbuf(ulong s)
{
uchar *p;
p = malloc(s);
if(p == nil)
sysfatal("malloc: %r");
return p;
}