18static size_t dataL_checkpos( lua_State *L,
const LuaData_t *ld,
long pos );
71 return (
LuaData_t *)lua_touserdata( L, ind );
84 luaL_typerror( L, ind, DATA_METATABLE );
99 luaL_getmetatable( L, DATA_METATABLE );
100 lua_setmetatable( L, -2 );
114 if ( lua_getmetatable( L, ind ) == 0 )
116 lua_getfield( L, LUA_REGISTRYINDEX, DATA_METATABLE );
119 if ( lua_rawequal( L, -1, -2 ) )
153 lua_pushboolean( L, 0 );
156 lua_pushboolean( L, ( memcmp( d1->
data, d2->
data, d1->
size ) == 0 ) );
171 size_t size = luaL_checklong( L, 1 );
172 const char *type = luaL_checkstring( L, 2 );
173 if ( strcmp( type,
"number" ) == 0 ) {
174 ld.
type = LUADATA_NUMBER;
175 ld.
elem =
sizeof( float );
177 return NLUA_ERROR( L, _(
"unknown data type '%s'" ), type );
184static size_t dataL_checkpos( lua_State *L,
const LuaData_t *ld,
long pos )
188 return NLUA_ERROR( L, _(
"position argument must be positive!" ) );
189 mpos = pos * ld->
elem;
190 if ( mpos >= ld->
size )
192 L, _(
"position argument out of bounds: %d of %d elements" ), pos,
208 long pos = luaL_checklong( L, 2 );
209 size_t mpos = dataL_checkpos( L, ld, pos );
210 char *data = ld->
data;
211 switch ( ld->
type ) {
213 lua_pushnumber( L, *( (
float *)( (
void *)&data[mpos] ) ) );
230 long pos = luaL_checklong( L, 2 );
231 size_t mpos = dataL_checkpos( L, ld, pos );
232 char *data = ld->
data;
234 switch ( ld->
type ) {
236 value = luaL_checknumber( L, 3 );
237 *( (
float *)( (
void *)&data[mpos] ) ) = value;
253 lua_pushnumber( L, ld->
size );
267 lua_pushlstring( L, ld->
data, ld->
size );
285 long dx = luaL_checklong( L, 3 ) * dest->
elem;
286 long sx = luaL_checklong( L, 4 ) * source->elem;
287 long sw = luaL_checklong( L, 5 ) * source->elem;
288 char *ddata = dest->
data;
289 const char *sdata = source->data;
292 if ( dx + sw > (
long)dest->
size )
294 L, _(
"size mismatch: out of bound access dest: %d of %d elements" ),
295 dx + sw, dest->
size );
296 else if ( sx + sw > (
long)source->size )
299 _(
"size mismatch: out of bound access of source: %d of %d elements" ),
300 sx + sw, source->size );
303 memcpy( &ddata[dx], &sdata[sx], sw );
306 lua_pushvalue( L, 1 );
326 double alpha = luaL_checknumber( L, 3 );
327 double beta = luaL_optnumber( L, 4, 1. - alpha );
328 double bias = luaL_optnumber( L, 5, 0. );
335 L, _(
"size mismatch: A has %d elements but B has %d elements" ),
337 if ( A->
type != LUADATA_NUMBER || B->
type != LUADATA_NUMBER )
338 return NLUA_ERROR( L, _(
"%s is only implemented for number types" ),
349 a = (
float *)A->
data;
350 b = (
float *)B->
data;
351 o = (
float *)out.
data;
352 for ( i = 0; i < n; i++ )
353 o[i] = a[i] * alpha + b[i] * beta + bias;
375 long iw = luaL_checklong( L, 2 );
376 long ih = luaL_checklong( L, 3 );
378 long kw = luaL_checklong( L, 5 );
379 long kh = luaL_checklong( L, 6 );
381 int kw2, kh2, bw, bh, ow, oh;
382 const float *I = (
const float *)lI->
data;
383 const float *K = (
const float *)lK->
data;
387 if ( iw * ih * 4 * lI->
elem != lI->
size )
389 L, _(
"size mismatch for data: got %dx%dx4x%d, expected %d" ), iw, ih,
391 if ( kw * kh * 4 * lK->
elem != lK->
size )
393 L, _(
"size mismatch for data: got %dx%dx4x%d, expected %d" ), kw, kh,
395 if ( lI->
type != LUADATA_NUMBER || lK->
type != LUADATA_NUMBER )
396 return NLUA_ERROR( L, _(
"%s is only implemented for number types" ),
400 kw2 = ( kw - 1 ) / 2;
401 kh2 = ( kh - 1 ) / 2;
410 O = (
float *)out.
data;
412#define POS( U, V, W ) ( 4 * ( ( V ) * ( W ) + ( U ) ) )
416 B = calloc( bw * bh * 4,
sizeof(
float ) );
417 for (
int v = 0; v < ih; v++ )
418 memcpy( &B[POS( kw2, v + kh2, bw )], &I[POS( 0, v, iw )],
419 4 *
sizeof(
float ) * iw );
422 for (
int v = 0; v < oh; v++ ) {
423 for (
int u = 0; u < ow; u++ ) {
424 for (
int kv = 0; kv < kh; kv++ ) {
425 for (
int ku = 0; ku < kw; ku++ ) {
428 for (
int p = 0; p < 4; p++ )
429 O[POS( u, v, ow ) + p] +=
430 B[POS( bu, bv, bw ) + p] * K[POS( ku, kv, kw ) + p];
442 lua_pushinteger( L, ow );
443 lua_pushinteger( L, oh );
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.