Esempio n. 1
0
const char* luaT_newmetatable(lua_State *L, const char *tname, const char *parenttname,
                              lua_CFunction constructor, lua_CFunction destructor, lua_CFunction factory)
{
  lua_pushcfunction(L, luaT_lua_newmetatable);
  lua_pushstring(L, tname);
  (parenttname ? lua_pushstring(L, parenttname) : lua_pushnil(L));
  (constructor ? lua_pushcfunction(L, constructor) : lua_pushnil(L));
  (destructor ? lua_pushcfunction(L, destructor) : lua_pushnil(L));
  (factory ? lua_pushcfunction(L, factory) : lua_pushnil(L));
  lua_call(L, 5, 1);
  return luaT_typenameid(L, tname);
}
Esempio n. 2
0
File: luaT.c Progetto: BwRy/torch7
const char* luaT_newlocalmetatable(lua_State *L, const char *tname, const char *parent_tname,
                                   lua_CFunction constructor, lua_CFunction destructor, lua_CFunction factory, int moduleidx)
{
  lua_pushcfunction(L, luaT_lua_newmetatable);
  lua_pushstring(L, tname);
  (parent_tname ? lua_pushstring(L, parent_tname) : lua_pushnil(L));
  (constructor ? lua_pushcfunction(L, constructor) : lua_pushnil(L));
  (destructor ? lua_pushcfunction(L, destructor) : lua_pushnil(L));
  (factory ? lua_pushcfunction(L, factory) : lua_pushnil(L));
  (moduleidx > 0 ? lua_pushvalue(L, moduleidx) : lua_pushnil(L));
  lua_call(L, 6, 1);
  return luaT_typenameid(L, tname);
}
Esempio n. 3
0
    static int parse(lua_State *L)
    {
        const char* id = luaT_typenameid(L, "torch.FloatTensor"); //Get float
        THFloatTensor *tensor = (THFloatTensor*) luaT_checkudata(L, 1, id); //Check if float
        float *input_data = THFloatTensor_data(tensor); //Pointer to tensor region

        float threshold = lua_tonumber(L, 2); //Threshold sent by lua
        int table_blobs = 3;
        int idx = lua_objlen(L, 3) + 1;
        float scale = lua_tonumber(L, 4);  //Which scale was this called for?

        // loop over pixels
        int x,y;
        for (y=0; y<tensor->size[0]; y++) {
            for (x=0; x<tensor->size[1]; x++) {
                float val = THFloatTensor_get2d(tensor, y, x);
                if (val > threshold) {
                    // entry = {}
                    lua_newtable(L);
                    int entry = lua_gettop(L);

                    // entry[1] = x
                    lua_pushnumber(L, x);
                    lua_rawseti(L, entry, 1);

                    // entry[2] = y
                    lua_pushnumber(L, y);
                    lua_rawseti(L, entry, 2);

                    // entry[3] = scale
                    lua_pushnumber(L, scale);
                    lua_rawseti(L, entry, 3);

                    // blobs[idx] = entry; idx = idx + 1
                    lua_rawseti(L, table_blobs, idx++);
                }
            }
        }
        return 1;
    }
Esempio n. 4
0
static int luafunc_load(lua_State *L)
{
	THFloatTensor *t = 0;
	const char *tname = luaT_typename(L, 1);
	int i, index = lua_tointeger(L, 2);

	if(max == 0)
		luaL_error(L, "fastimage.init: call init first");
	if(index > nsizes)
		luaL_error(L, "Invalid size index %d", index);
	index--;
	if(index < 0)
		index = 0;
	if(tname && !strcmp(tname, "torch.FloatTensor"))
	{
		t = luaT_toudata(L, 1, luaT_typenameid(L, "torch.FloatTensor"));
		if(t->nDimension == 4 && t->size[1] == 3)
		{
			if(nsizes == 1)
			{
				sizes[0].width = t->size[3];
				sizes[0].height = t->size[2];
				max = t->size[0];
			} else if(sizes[0].width != t->size[3] || sizes[0].height != t->size[2] ||
				max != t->size[0])
				t = 0;
		} else t = 0;
	}
	if(!index)
	{
		for(i = 0; i < max; i++)
			if(images[i].bitmap)
			{
				free(images[i].bitmap);
				images[i].bitmap = 0;
			}
		for(i = 0; i < max; i++)
		{
			if(loadnextimage(images + i))
				break;
		}
		if(i == 0)
		{
			lprintf("Nothing found\n");
			return 0;
		}
		if(i < max)
		{
			max = i;
			if(t)
				t = THFloatTensor_newNarrow(t, 0, 0, i);
		}	
	}
	for(i = 0; i < max; i++)
	{
		if(nsizes == 1 && (!sizes[0].width || !sizes[0].height))
		{
			lprintf("Set width = %d, height = %d\n", images[i].width, images[i].height);
			sizes[0].width = images[i].width;
			sizes[0].height = images[i].height;
		}
		if(!t)
			t = THFloatTensor_newWithSize4d(max, 3, sizes[index].height, sizes[index].width);
		uint8_t *rescaled = scale(images + i, sizes[index].width, sizes[index].height);
		rgb_tofloat(THFloatTensor_data(t) + i * t->stride[0], t->stride[1], t->stride[2], rescaled, sizes[index].width, sizes[index].height);
		if(rescaled != images[i].bitmap)
			free(rescaled);
		if(nsizes == 1 && images[i].bitmap)
		{
			// It's not necessary to keep all the images in memory, if there is only one size
			free(images[i].bitmap);
			images[i].bitmap = 0;
		}
	}
	lprintf("%d x 3 x %d x %d tensor returned\n", i, sizes[index].height, sizes[index].width);
	luaT_pushudata(L, t, "torch.FloatTensor");
	lua_createtable(L, max, 0);
	for(i = 0; i < max; i++)
	{
		lua_pushinteger(L, i+1);

		lua_createtable(L, 0, 3);
		lua_pushstring(L, "filename");
		lua_pushstring(L, images[i].filename);
		lua_settable(L, -3);
		lua_pushstring(L, "width");
		lua_pushinteger(L, images[i].width);
		lua_settable(L, -3);
		lua_pushstring(L, "height");
		lua_pushinteger(L, images[i].height);
		lua_settable(L, -3);

		lua_settable(L, -3);
	}
	return 2;
}
Esempio n. 5
0
static int luafunc_init(lua_State *L)
{
	struct stat st;
	const char *path = lua_tostring(L, 1);
	max = lua_tointeger(L, 2);

	if(!path)
		luaL_error(L, "fastimage.init: path has to be a string");
	if(max < 1)
		luaL_error(L, "fastimage.init: max has to be a positive number");
	strcpy(initpath, path);
	const char *tname = luaT_typename(L, 3);
	if(images)
	{
		int i;

		for(i = 0; i < max; i++)
			if(images[i].bitmap)
				free(images[i].bitmap);
		free(images);
		images = 0;
	}
	if(sizes)
	{
		free(sizes);
		sizes = 0;
	}
	nsizes = 0;
	if(tname && !strcmp(tname, "torch.FloatTensor"))
	{
		THFloatTensor *t = luaT_toudata(L, 3, luaT_typenameid(L, "torch.FloatTensor"));
		if(t->nDimension == 2 && t->size[1] == 2)
		{
			int i;
			nsizes = t->size[0];
			sizes = (imgsize_t *)malloc(nsizes * sizeof(imgsize_t));
			float *data = THFloatTensor_data(t);
			for(i = 0; i < nsizes; i++)
			{
				sizes[i].width = data[i * t->stride[0]];
				sizes[i].height = data[i * t->stride[0] + 1];
			}
			if(lua_isnumber(L, 4))
				greylevel = (int)(255 * lua_tonumber(L, 4));
			else greylevel = -1;
		} else t = 0;
	} else {
		nsizes = 1;
		sizes = (imgsize_t *)malloc(sizeof(imgsize_t));
		sizes[0].width = lua_tointeger(L, 3);
		sizes[0].height = lua_tointeger(L, 4);
		if(lua_isnumber(L, 5))
			greylevel = (int)(255 * lua_tonumber(L, 5));
		else greylevel = -1;
	}
	images = (img_t *)calloc(max, sizeof(img_t));

	lprintf("fastimage.init(%s, %d, %d, %d, %d)\n", path, max, sizes[0].width, sizes[0].height, greylevel);
	terminate = 0;
	if(dir)
	{
		closedir(dir);
		dir = 0;
	}
	if(!stat(path, &st))
	{
		if(S_ISREG(st.st_mode))
			return 0;
		else if(S_ISDIR(st.st_mode))
		{
			lprintf("opendir %s\n", path);
			dir = opendir(path);
			if(!dir)
				luaL_error(L, "fastimage.init: failed to open directory %s", path);
			return 0;
		} else luaL_error(L, "fastimage.init: %s is neither a file, nor a directory", path);
	} else luaL_error(L, "fastimage.init: Cannot stat %s", path);
	return 0;
}