-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua_stb.h
66 lines (54 loc) · 1.75 KB
/
lua_stb.h
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 <stdlib.h>
#include <string.h>
#ifndef release
#define release(ptr) free(ptr)
#endif
#ifndef allocate
#define allocate(size) malloc(size)
#endif
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#ifndef ImageDataDef
typedef struct ImageData
{
int w;
int h;
int n;
unsigned char * data;
} ImageData;
static int luaL_setimagedata(lua_State* L, const unsigned char * data, int w, int h, int n) {
ImageData* imageData = lua_newuserdata(L, sizeof(ImageData));
imageData->w = w;
imageData->h = h;
imageData->n = n;
const unsigned char * imageDataChar = allocate(w * h * n);
imageData->data = memcpy(imageDataChar, data, w * h * n);
luaL_setmetatable(L, "__ImageData__");
return 1;
}
static int image_data_index (lua_State* L) {
ImageData* imageData = luaL_checkudata(L, 1, "__ImageData__");
lua_Integer index = luaL_checkinteger(L, 2);
if(index - 1 < imageData->w * imageData->h * imageData->n ){
lua_pushinteger(L, imageData->data[index - 1]);
}
return 1;
}
static int image_data_newindex (lua_State* L) {
ImageData* imageData = luaL_checkudata(L, 1, "__ImageData__");
lua_Integer index = luaL_checkinteger(L, 2);
lua_Integer value = luaL_checkinteger(L, 3);
if (value < 0 || value > 255) {
luaL_error(L, "value is out of range[0, 255]");
}
imageData->data[index - 1] = value;
return 0;
}
static int image_data_gc (lua_State* L) {
ImageData* imageData = luaL_checkudata(L, 1, "__ImageData__");
release(imageData->data);
return 0;
}
#define ImageDataDef 1
#endif