-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpixels.c
97 lines (84 loc) · 1.79 KB
/
pixels.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
#include "a.h"
Pixels *pixels;
Pixels*
checkpixels(lua_State *L)
{
void *p;
p = luaL_checkudata(L, 1, "slug.pixels");
luaL_argcheck(L, p != nil, 1, "`pixels` expected");
return p;
}
int
pixelstostring(lua_State *L)
{
checkpixels(L);
lua_pushfstring(L, "pixels(%dx%d)", width, height);
return 1;
}
int
pixelsget(lua_State *L)
{
Pixels *p;
int i;
p = checkpixels(L);
i = luaL_checkinteger(L, 2);
luaL_argcheck(L, 1 <= i && i <= (width*height), 2, "index out of range");
i = (i-1)*4;
lua_newtable(L);
lua_pushinteger(L, p->data[i+2]);
lua_setfield(L, -2, "r");
lua_pushinteger(L, p->data[i+1]);
lua_setfield(L, -2, "g");
lua_pushinteger(L, p->data[i+0]);
lua_setfield(L, -2, "b");
return 1;
}
int
pixelsset(lua_State *L)
{
Pixels *p;
int i, r, g, b;
p = checkpixels(L);
i = luaL_checkinteger(L, 2);
luaL_argcheck(L, 1 <= i && i <= width*height, 2, "index out of range");
if(lua_istable(L, 3) == 0)
luaL_argerror(L, 3, "color table expected");
lua_pushstring(L, "r");
lua_gettable(L, 3);
r = luaL_checkinteger(L, -1);
lua_pushstring(L, "g");
lua_gettable(L, 3);
g = luaL_checkinteger(L, -1);
lua_pushstring(L, "b");
lua_gettable(L, 3);
b = luaL_checkinteger(L, -1);
i = (i-1)*4;
p->data[i + 2] = r;
p->data[i + 1] = g;
p->data[i + 0] = b;
return 0;
}
static const struct luaL_Reg pixelsmethods [] = {
{ "__tostring", pixelstostring },
{ "__index", pixelsget },
{ "__newindex", pixelsset },
{ nil, nil},
};
int
registerpixels(lua_State *L)
{
luaL_newmetatable(L, "slug.pixels");
luaL_setfuncs(L, pixelsmethods, 0);
lua_pop(L, 1);
return 0;
}
void
lpushpixels(lua_State *L)
{
int n;
n = sizeof(Pixels) + (width*height*4-1)*sizeof(uchar);
pixels = lua_newuserdata(L, n);
luaL_setmetatable(L, "slug.pixels");
pixels->ndata = width*height*4;
lua_setglobal(L, "pixels");
}