21static size_t dataL_checkpos( lua_State *L,
const LuaData_t *ld,
long pos );
74 return (
LuaData_t*) lua_touserdata(L,ind);
87 luaL_typerror(L, ind, DATA_METATABLE);
102 luaL_getmetatable(L, DATA_METATABLE);
103 lua_setmetatable(L, -2);
117 if (lua_getmetatable(L,ind)==0)
119 lua_getfield(L, LUA_REGISTRYINDEX, DATA_METATABLE);
122 if (lua_rawequal(L, -1, -2))
156 lua_pushboolean( L, 0 );
159 lua_pushboolean( L, (memcmp( d1->
data, d2->
data, d1->
size)==0) );
174 size_t size = luaL_checklong(L,1);
175 const char *type = luaL_checkstring(L,2);
176 if (strcmp(type,
"number")==0) {
177 ld.
type = LUADATA_NUMBER;
178 ld.
elem =
sizeof(float);
181 return NLUA_ERROR(L, _(
"unknown data type '%s'"), type);
188static size_t dataL_checkpos( lua_State *L,
const LuaData_t *ld,
long pos )
192 return NLUA_ERROR(L, _(
"position argument must be positive!"));
193 mpos = pos * ld->
elem;
194 if (mpos >= ld->
size)
195 return NLUA_ERROR(L, _(
"position argument out of bounds: %d of %d elements"), pos, ld->
size/ld->
elem);
210 long pos = luaL_checklong(L,2);
211 size_t mpos = dataL_checkpos( L, ld, pos );
212 char *data = ld->
data;
215 lua_pushnumber(L, *((
float*)((
void*)&data[mpos])));
232 long pos = luaL_checklong(L,2);
233 size_t mpos = dataL_checkpos( L, ld, pos );
234 char *data = ld->
data;
238 value = luaL_checknumber(L,3);
239 *((
float*)((
void*)&data[mpos])) = value;
255 lua_pushnumber(L, ld->
size);
269 lua_pushlstring(L, ld->
data, ld->
size);
287 long dx = luaL_checklong(L,3) * dest->
elem;
288 long sx = luaL_checklong(L,4) * source->elem;
289 long sw = luaL_checklong(L,5) * source->elem;
290 char *ddata = dest->
data;
291 const char *sdata = source->data;
294 if (dx+sw > (
long)dest->
size)
295 return NLUA_ERROR(L, _(
"size mismatch: out of bound access dest: %d of %d elements"), dx+sw, dest->
size);
296 else if (sx+sw > (
long)source->size)
297 return NLUA_ERROR(L, _(
"size mismatch: out of bound access of source: %d of %d elements"), sx+sw, source->size);
300 memcpy( &ddata[dx], &sdata[sx], sw );
323 double alpha = luaL_checknumber(L,3);
324 double beta = luaL_optnumber(L,4,1.-alpha);
325 double bias = luaL_optnumber(L,5,0.);
331 return NLUA_ERROR(L, _(
"size mismatch: A has %d elements but B has %d elements"), A->
size, B->
size );
332 if (A->
type != LUADATA_NUMBER || B->
type != LUADATA_NUMBER)
333 return NLUA_ERROR(L, _(
"%s is only implemented for number types"), __func__);
345 o = (
float*)out.
data;
347 o[i] = a[i]*alpha + b[i]*beta + bias;
369 long iw = luaL_checklong(L,2);
370 long ih = luaL_checklong(L,3);
372 long kw = luaL_checklong(L,5);
373 long kh = luaL_checklong(L,6);
375 int kw2,kh2, bw,bh, ow,oh;
376 const float *I = (
const float*)lI->
data;
377 const float *K = (
const float*)lK->
data;
382 return NLUA_ERROR(L,_(
"size mismatch for data: got %dx%dx4x%d, expected %d"), iw, ih, lI->
elem, lI->
size);
384 return NLUA_ERROR(L,_(
"size mismatch for data: got %dx%dx4x%d, expected %d"), kw, kh, lK->
elem, lK->
size);
385 if (lI->
type != LUADATA_NUMBER || lK->
type != LUADATA_NUMBER)
386 return NLUA_ERROR(L, _(
"%s is only implemented for number types"), __func__);
399 O = (
float*)out.
data;
401#define POS(U,V,W) (4*((V)*(W)+(U)))
405 B = calloc( bw*bh*4,
sizeof(
float) );
406 for (
int v=0; v<ih; v++)
407 memcpy( &B[ POS(kw2, v+kh2, bw) ],
408 &I[ POS( 0, v, iw) ],
409 4*
sizeof(
float)*iw );
412 for (
int v=0; v<oh; v++) {
413 for (
int u=0; u<ow; u++) {
414 for (
int kv=0; kv<kh; kv++) {
415 for (
int ku=0; ku<kw; ku++) {
418 for (
int p=0; p<4; p++)
419 O[ POS( u, v, ow )+p ] +=
420 B[ POS( bu, bv, bw )+p ]
421 * K[ POS( ku, kv, kw )+p ];
433 lua_pushinteger(L,ow);
434 lua_pushinteger(L,oh);
Header file with generic functions and naev-specifics.
static int dataL_paste(lua_State *L)
Writes the contents of "source" into "dest".
static int dataL_addWeighted(lua_State *L)
Returns alpha*A + beta*B + bias.
static int dataL_eq(lua_State *L)
Compares two datas to see if they are the same.
LuaData_t * luaL_checkdata(lua_State *L, int ind)
Gets data at index or raises error if there is no data at index.
static int dataL_get(lua_State *L)
Gets the value of an element.
static int dataL_convolve2d(lua_State *L)
Does a convolution. You'd rather be writing shaders, right?
static int dataL_gc(lua_State *L)
Frees a data.
int lua_isdata(lua_State *L, int ind)
Checks to see if ind is a data.
LuaData_t * lua_pushdata(lua_State *L, LuaData_t data)
Pushes a data on the stack.
static const luaL_Reg dataL_methods[]
static int dataL_new(lua_State *L)
Opens a new data.
LuaData_t * lua_todata(lua_State *L, int ind)
Lua bindings to interact with datas.
static int dataL_getSize(lua_State *L)
Gets the number of elements.
static int dataL_set(lua_State *L)
Sets the value of an element.
static int dataL_getString(lua_State *L)
Returns the data contents as a string.
int nlua_loadData(nlua_env env)
Loads the data library.