-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage.c
99 lines (83 loc) · 1.54 KB
/
image.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "a.h"
#define IMAGE "Image"
typedef struct LImage LImage;
struct LImage
{
Image *i;
};
void pushimage(lua_State *L, Image *i)
{
LImage *l;
l = (LImage*)lua_newuserdata(L, sizeof(LImage));
luaL_getmetatable(L, IMAGE);
lua_setmetatable(L, -2);
l->i = i;
}
Image*
checkimage(lua_State *L, int index)
{
LImage *l;
l = (LImage*)luaL_checkudata(L, index, IMAGE);
luaL_argcheck(L, l != NULL, index, "Image expected");
return l->i;
}
Image*
optimage(lua_State *L, int index)
{
if(lua_isnil(L, index))
return nil;
return checkimage(L, index);
}
static int
image__gc(lua_State *L)
{
Image *i;
i = checkimage(L, 1);
if(i == screen) {
lua_pushboolean(L, 0);
return 1;
}
/* TODO freeimage */
lua_pushboolean(L, 1);
return 1;
}
static int
image__tostring(lua_State *L)
{
void *p;
p = lua_touserdata(L, 1);
lua_pushfstring(L, "image: %p", p);
return 1;
}
static int
image__index(lua_State *L)
{
Image *i;
const char *s;
i = checkimage(L, 1);
s = luaL_checkstring(L, 2);
if(!strncmp(s, "r", 1))
pushrect(L, i->r);
else if(!strncmp(s, "clipr", 5))
pushrect(L, i->clipr);
else if(!strncmp(s, "chan", 4))
lua_pushinteger(L, i->chan);
else if(!strncmp(s, "depth", 5))
lua_pushinteger(L, i->depth);
else if(!strncmp(s, "repl", 4))
lua_pushinteger(L, i->repl);
else
return 0;
return 1;
}
static const struct luaL_Reg image_funcs[] = {
{ "__gc", image__gc },
{ "__tostring", image__tostring },
{ "__index", image__index },
{ NULL, NULL },
};
void
registerimagemeta(lua_State *L)
{
createmetatable(L, IMAGE, image_funcs);
}