DLL_EXPORT int luaopen_libkinect(lua_State *L) { torch_FloatTensor_id = luaT_checktypename2id(L, "torch.FloatTensor"); torch_DoubleTensor_id = luaT_checktypename2id(L, "torch.DoubleTensor"); kinect_FloatInit(L); kinect_DoubleInit(L); return 1; }
extern "C" void svm_QPSolver_init(lua_State *L) { torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); torch_IntStorage_id = luaT_checktypename2id(L, "torch.IntStorage"); QPSolver_id = luaT_newmetatable(L, "svm.QPSolver", NULL, QPSolver_new, QPSolver_free, NULL); luaL_register(L, NULL, QPSolver__); lua_pop(L, 1); }
DLL_EXPORT int luaopen_libjpeg(lua_State *L) { torch_FloatTensor_id = luaT_checktypename2id(L, "torch.FloatTensor"); torch_DoubleTensor_id = luaT_checktypename2id(L, "torch.DoubleTensor"); libjpeg_FloatMain_init(L); libjpeg_DoubleMain_init(L); luaL_register(L, "libjpeg.double", libjpeg_DoubleMain__); luaL_register(L, "libjpeg.float", libjpeg_FloatMain__); return 1; }
// frame grabber static int l_grabFrame (lua_State *L) { // Get Tensor's Info const int idx = lua_tonumber(L, 1); THFloatTensor * tensor = luaT_checkudata(L, 2, luaT_checktypename2id(L, "torch.FloatTensor")); // grab frame frame[idx] = cvQueryFrame ( capture[idx] ); if( !frame[idx] ) { perror("could not query OpenCV capture"); } // resize given tensor THFloatTensor_resize3d(tensor, 3, frame[idx]->height, frame[idx]->width); // copy to tensor int m0 = tensor->stride[1]; int m1 = tensor->stride[2]; int m2 = tensor->stride[0]; unsigned char *src = frame[idx]->imageData; float *dst = THFloatTensor_data(tensor); int i, j, k; for (i=0; i < frame[idx]->height; i++) { for (j=0, k=0; j < frame[idx]->width; j++, k+=m1) { // red: dst[k] = src[i*frame[idx]->widthStep + j*frame[idx]->nChannels + 2]/255.; // green: dst[k+m2] = src[i*frame[idx]->widthStep + j*frame[idx]->nChannels + 1]/255.; // blue: dst[k+2*m2] = src[i*frame[idx]->widthStep + j*frame[idx]->nChannels + 0]/255.; } dst += m0; } return 0; }
void nn_LcEncoder_init(lua_State *L) { torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); nn_LcEncoder_id = luaT_newmetatable(L, "nn.LcEncoder", NULL, NULL, NULL, NULL); luaL_register(L, NULL, nn_LcEncoder__); lua_pop(L, 1); }
static void load_array_to_lua(lua_State *L, chtk::htkarray& arr){ int ndims = 2; //based on code from mattorch with stride fix int k; THLongStorage *size = THLongStorage_newWithSize(ndims); THLongStorage *stride = THLongStorage_newWithSize(ndims); THLongStorage_set(size,0 , arr.nsamples); THLongStorage_set(size,1,arr.samplesize/4*(2*arr.frm_ext+1)); THLongStorage_set(stride,1,1); THLongStorage_set(stride,0,arr.samplesize/4*(2*arr.frm_ext+1)); void * tensorDataPtr = NULL; size_t numBytes = 0; THFloatTensor *tensor = THFloatTensor_newWithSize(size, stride); tensorDataPtr = (void *)(THFloatTensor_data(tensor)); numBytes = THFloatTensor_nElement(tensor) * 4; luaT_pushudata(L, tensor, luaT_checktypename2id(L, "torch.FloatTensor")); // now copy the data assert(tensorDataPtr); memcpy(tensorDataPtr, (void *)(arr.data<void>()), numBytes); }
void nn_SpatialConvolution_init(lua_State *L) { torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); nn_SpatialConvolution_id = luaT_newmetatable(L, "nn.SpatialConvolution", NULL, NULL, NULL, NULL); luaL_register(L, NULL, nn_SpatialConvolution__); lua_pop(L, 1); }
void nn_TemporalSubSampling_init(lua_State *L) { torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); nn_TemporalSubSampling_id = luaT_newmetatable(L, "nn.TemporalSubSampling", NULL, NULL, NULL, NULL); luaL_register(L, NULL, nn_TemporalSubSampling__); lua_pop(L, 1); }
void lcairo_init(lua_State *L) { globalL=L; luaL_register(L, NULL, lcairo__); /* external classes used */ torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); /* cairo */ tCairo_id =luaT_newmetatable(L, "lcairo.tCairo", NULL, NULL, lcairo_cairo_destroy, NULL); /* surfaces */ /* tSurface and tSurfaceData are only ever refeneces - no garbage collection */ tSurface_id =luaT_newmetatable(L, "lcairo.tSurface", NULL, NULL, NULL, NULL); tSurfaceData_id=luaT_newmetatable(L, "lcairo.tSurfaceData", NULL, NULL, NULL, NULL); tImage_id =luaT_newmetatable(L, "lcairo.tImage", NULL, NULL, lcairo_image_destroy, NULL); tWindow_id =luaT_newmetatable(L, "lcairo.tWindow", NULL, NULL, lcairo_window_destroy, NULL); /* patterns */ /* May need tPattern and tPatternCreated:child tPattern to differentiate for garbage */ tPattern_id =luaT_newmetatable(L, "lcairo.tPattern", NULL, NULL, NULL, NULL); #ifdef LCAIRO_USE_READLINE rl_event_hook=readline_callback; #endif /* LCAIRO_USE_READLINE */ }
static int etherflow_(Api_send_tensor_byte_lua)(lua_State *L) { // get params THByteTensor *tensor = luaT_toudata(L, 1, luaT_checktypename2id(L, "torch.ByteTensor")); int size = THByteTensor_nElement(tensor); unsigned char *data = THByteTensor_data(tensor); etherflow_send_ByteTensor_C(data, size); return 0; }
static int send_tensor_lua_ack(lua_State *L) { /* get the arguments */ THTensor * tensorToSend = luaT_toudata(L, 1, luaT_checktypename2id(L, "torch.Tensor")); int size = tensorToSend->size[0] * tensorToSend->size[1]; double *data = tensorToSend->storage->data+tensorToSend->storageOffset; send_tensor_double_C_ack(data, size); return 0; }
static int send_tensor_byte_lua(lua_State *L) { // get params THByteTensor * tensorToSend = luaT_toudata(L, 1, luaT_checktypename2id(L, "torch.ByteTensor")); int size = tensorToSend->size[0]; unsigned char * data = tensorToSend->storage->data+tensorToSend->storageOffset; send_tensor_byte_C(data, size); return 0; }
static int send_tensor_float_lua(lua_State *L) { /* get the arguments */ THFloatTensor * tensorToSend = luaT_toudata(L, 1, luaT_checktypename2id(L, "torch.FloatTensor")); int size = tensorToSend->size[0] * tensorToSend->size[1]; float *data = tensorToSend->storage->data+tensorToSend->storageOffset; send_tensor_float_C(data, size); return 0; }
static int receive_tensor_float_lua(lua_State *L){ /* get the arguments */ THFloatTensor * result = luaT_toudata(L, 1, luaT_checktypename2id(L, "torch.FloatTensor")); float * data = result->storage->data+result->storageOffset; int size = result->size[0]*result->size[1]; receive_tensor_float_C(data, size, result->size[1]); return 0; }
static int receive_tensor_lua_ack(lua_State *L){ /* get the arguments */ THTensor * result = luaT_toudata(L, 1, luaT_checktypename2id(L, "torch.Tensor")); double * data = result->storage->data+result->storageOffset; int size = result->size[0]*result->size[1]; receive_tensor_double_C_ack(data, size, result->size[1]); return 0; }
int image_saturate(lua_State *L) { THTensor *input = luaT_checkudata(L, 1, luaT_checktypename2id(L, "torch.Tensor")); THTensor *output = input; TH_TENSOR_APPLY2(double, output, double, input, \ *output_p = (*input_p < 0) ? 0 : (*input_p > 1) ? 1 : *input_p;) return 1;
void torch_MemoryFile_init(lua_State *L) { torch_CharStorage_id = luaT_checktypename2id(L, "torch.CharStorage"); torch_MemoryFile_id = luaT_newmetatable(L, "torch.MemoryFile", "torch.File", torch_MemoryFile_new, torch_MemoryFile_free, NULL); luaL_register(L, NULL, torch_MemoryFile__); lua_pop(L, 1); }
static int dist_smr(lua_State * L) { // get args const void* torch_FloatTensor_id = luaT_checktypename2id(L, "torch.FloatTensor"); THFloatTensor *output_ptr = luaT_checkudata(L, 1, torch_FloatTensor_id); THFloatTensor *input_ptr = luaT_checkudata(L, 2, torch_FloatTensor_id); THFloatTensor *kernel_ptr = luaT_checkudata(L, 3, torch_FloatTensor_id); float dynamic = lua_tonumber(L, 4); int begin_x = lua_tonumber(L, 5); int end_x = lua_tonumber(L, 6); int begin_y = lua_tonumber(L, 7); int end_y = lua_tonumber(L, 8); // get raw pointers float *output = THFloatTensor_data(output_ptr); float *input = THFloatTensor_data(input_ptr); float *kernel = THFloatTensor_data(kernel_ptr); // dims int kheight = kernel_ptr->size[0]; int kwidth = kernel_ptr->size[1]; //strides long *is = input_ptr->stride; long *ks = kernel_ptr->stride; long *os = output_ptr->stride; // similarity matching ratio (SMR) int i, j, x, y, pos; float probability; float distance; for(y = begin_y; y < end_y; y++) { for(x = begin_x; x < end_x; x++) { pos = y*is[0]+x*is[1]; probability = 0; for(j=0; j< kheight; j++) { for(i=0; i< kwidth; i++) { distance = abs(input[ pos+j*is[0]+i*is[1] ]- kernel[ j*ks[0]+i*ks[1] ]); if (distance<dynamic/2) probability = probability + exp(-2*distance); } } output[y*os[0]+x*os[1]] = probability; } } lua_newtable(L); // result = {} int result = lua_gettop(L); return 0; }
void torch_File_init(lua_State *L) { torch_CharStorage_id = luaT_checktypename2id(L, "torch.CharStorage"); torch_ShortStorage_id = luaT_checktypename2id(L, "torch.ShortStorage"); torch_IntStorage_id = luaT_checktypename2id(L, "torch.IntStorage"); torch_LongStorage_id = luaT_checktypename2id(L, "torch.LongStorage"); torch_FloatStorage_id = luaT_checktypename2id(L, "torch.FloatStorage"); torch_DoubleStorage_id = luaT_checktypename2id(L, "torch.DoubleStorage"); torch_File_id = luaT_newmetatable(L, "torch.File", NULL, NULL, NULL, NULL); luaL_register(L, NULL, torch_File__); lua_pop(L, 1); }
int luaopen_libqttorch(lua_State *L) { // load module 'qt' if (luaL_dostring(L, "require 'qt'")) lua_error(L); // load modules 'torch' if (luaL_dostring(L, "require 'torch'")) lua_error(L); torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); // enrichs QImage luaQ_pushmeta(L, QMetaType::QImage); luaQ_getfield(L, -1, "__metatable"); luaL_register(L, 0, qttorch_qimage_lib); return 0; }
void cutorch_CudaStorage_init(lua_State* L) { /* the ids */ torch_ByteStorage_id = luaT_checktypename2id(L, "torch.ByteStorage"); torch_CharStorage_id = luaT_checktypename2id(L, "torch.CharStorage"); torch_ShortStorage_id = luaT_checktypename2id(L, "torch.ShortStorage"); torch_IntStorage_id = luaT_checktypename2id(L, "torch.IntStorage"); torch_LongStorage_id = luaT_checktypename2id(L, "torch.LongStorage"); torch_FloatStorage_id = luaT_checktypename2id(L, "torch.FloatStorage"); torch_DoubleStorage_id = luaT_checktypename2id(L, "torch.DoubleStorage"); /* the standard stuff */ torch_CudaStorage_init(L); /* the copy methods */ { int i; const void* ids[8] = {torch_ByteStorage_id, torch_CharStorage_id, torch_ShortStorage_id, torch_IntStorage_id, torch_LongStorage_id, torch_FloatStorage_id, torch_DoubleStorage_id, torch_CudaStorage_id}; static int (*funcs[8])(lua_State*) = {cutorch_ByteStorage_copy, cutorch_CharStorage_copy, cutorch_ShortStorage_copy, cutorch_IntStorage_copy, cutorch_LongStorage_copy, cutorch_FloatStorage_copy, cutorch_DoubleStorage_copy, cutorch_CudaStorage_copy}; for(i = 0; i < 8; i++) { luaT_pushmetaclass(L, ids[i]); lua_pushcfunction(L, funcs[i]); lua_setfield(L, -2, "copy"); lua_pop(L, 1); } } }
DLL_EXPORT int luaopen_libjpeg(lua_State *L) { torch_Tensor_id = luaT_checktypename2id(L, "torch.Tensor"); luaL_register(L, "jpeg", jpeg__); return 1; }
template<> inline THTensor<double> FromLuaStack<THTensor<double> >(lua_State* L, int i) { return THTensor<double>((TH<double>::CTensor*)luaT_checkudata(L, i, luaT_checktypename2id(L, "torch.DoubleTensor"))); }
template<> inline THTensor<float> FromLuaStack<THTensor<float> >(lua_State* L, int i) { return THTensor<float>((TH<float>::CTensor*)luaT_checkudata(L, i, luaT_checktypename2id(L, "torch.FloatTensor"))); }
static void load_array_to_lua(lua_State *L, cnpy::NpyArray& arr){ int ndims = arr.shape.size(); //based on code from mattorch with stride fix int k; THLongStorage *size = THLongStorage_newWithSize(ndims); THLongStorage *stride = THLongStorage_newWithSize(ndims); for (k=0; k<ndims; k++) { THLongStorage_set(size, k, arr.shape[k]); if (k > 0) THLongStorage_set(stride, ndims-k-1, arr.shape[ndims-k]*THLongStorage_get(stride,ndims-k)); else THLongStorage_set(stride, ndims-k-1, 1); } void * tensorDataPtr = NULL; size_t numBytes = 0; if ( arr.arrayType == 'f' ){ // float32/64 if ( arr.word_size == 4 ){ //float32 THFloatTensor *tensor = THFloatTensor_newWithSize(size, stride); tensorDataPtr = (void *)(THFloatTensor_data(tensor)); numBytes = THFloatTensor_nElement(tensor) * arr.word_size; luaT_pushudata(L, tensor, luaT_checktypename2id(L, "torch.FloatTensor")); }else if ( arr.word_size == 8){ //float 64 THDoubleTensor *tensor = THDoubleTensor_newWithSize(size, stride); tensorDataPtr = (void *)(THDoubleTensor_data(tensor)); numBytes = THDoubleTensor_nElement(tensor) * arr.word_size; luaT_pushudata(L, tensor, luaT_checktypename2id(L, "torch.DoubleTensor")); } }else if ( arr.arrayType == 'i' || arr.arrayType == 'u' ){ // does torch have unsigned types .. need to look if ( arr.word_size == 1 ){ //int8 THByteTensor *tensor = THByteTensor_newWithSize(size, stride); tensorDataPtr = (void *)(THByteTensor_data(tensor)); numBytes = THByteTensor_nElement(tensor) * arr.word_size; luaT_pushudata(L, tensor, luaT_checktypename2id(L, "torch.ByteTensor")); }else if ( arr.word_size == 2 ){ //int16 THShortTensor *tensor = THShortTensor_newWithSize(size, stride); tensorDataPtr = (void *)(THShortTensor_data(tensor)); numBytes = THShortTensor_nElement(tensor) * arr.word_size; luaT_pushudata(L, tensor, luaT_checktypename2id(L, "torch.ShortTensor")); }else if ( arr.word_size == 4 ){ //int32 THIntTensor *tensor = THIntTensor_newWithSize(size, stride); tensorDataPtr = (void *)(THIntTensor_data(tensor)); numBytes = THIntTensor_nElement(tensor) * arr.word_size; luaT_pushudata(L, tensor, luaT_checktypename2id(L, "torch.IntTensor")); }else if ( arr.word_size == 8){ //long 64 THLongTensor *tensor = THLongTensor_newWithSize(size, stride); tensorDataPtr = (void *)(THLongTensor_data(tensor)); numBytes = THLongTensor_nElement(tensor) * arr.word_size; luaT_pushudata(L, tensor, luaT_checktypename2id(L, "torch.LongTensor")); } }else{ printf("array type unsupported"); throw std::runtime_error("unsupported data type"); } // now copy the data assert(tensorDataPtr); memcpy(tensorDataPtr, (void *)(arr.data<void>()), numBytes); }
void lab_utils_init(lua_State *L) { torch_LongStorage_id = luaT_checktypename2id(L, "torch.LongStorage"); }
// Stitch takes args. // pano - a torch tensor in RGB with dims (3 x height x width) // // offset_map - a torch tensor same h and w as pano storing offsets and // and image indices. The two feaure dimentions are: // -- image number (starting at 1) and // -- bit offset in image tensor // nimages - is the number of images use to make the panorama // image1, ... imagen - are the image in a torch tensor static int Lstitch_(stitch)(lua_State *L) { int nargs = lua_gettop(L); THTensor *pano = (THTensor *)luaT_checkudata(L, 1, torch_(Tensor_id)); THLongTensor *offset_map = (THLongTensor *)luaT_checkudata(L, 2, luaT_checktypename2id(L, "torch.LongTensor")); THTensor *images[MAXIMAGES]; int i = 0; long npixels = offset_map->size[1]*offset_map->size[2]; real *pano_pt = THTensor_(data)(pano); long *offset_pt = THLongTensor_data(offset_map); real *images_pt[MAXIMAGES]; long images_npixels[MAXIMAGES]; long images_Goff[MAXIMAGES]; long images_Boff[MAXIMAGES]; real * panoR = pano_pt; real * panoG = pano_pt + pano->stride[0]; real * panoB = pano_pt + (2*pano->stride[0]); real * curImg_pt = NULL; long unsigned int XYoffset = 0; long * offImg = offset_pt; long * offIndexXY = offset_pt + offset_map->stride[0]; int nimages = 0; long cImgOff = 0; /* finish processing input image tensors */ /* either you can pass a table */ /* or a number and variable length of args */ if (nargs == 3){ if (lua_istable(L,3)){ nimages = lua_objlen (L, 3); /* table is in the stack at index 3 */ lua_pushnil(L); /* first key */ i = 0; while (lua_next(L, 3) != 0) { /* 'key' (at index -2) and 'value' (at index -1) */ images[i] = (THTensor *)luaT_checkudata(L, -1, torch_(Tensor_id)); images_npixels[i] = images[i]->size[1]*images[i]->size[2]; images_Goff[i] = images[i]->stride[0]; images_Boff[i] = 2*images[i]->stride[0]; images_pt[i] = THTensor_(data)(images[i]); /* removes 'value'; keeps 'key' for next iteration */ lua_pop(L, 1); i = i+1; } } else { lua_pushstring(L, "with 3 args last argument is a table"); lua_error(L); } } else { nimages = lua_tonumber(L,3); for(i=0;i<nimages;i++){ images[i] = (THTensor *)luaT_checkudata(L, i+4, torch_(Tensor_id)); images_npixels[i] = images[i]->size[1]*images[i]->size[2]; images_Goff[i] = images[i]->stride[0]; images_Boff[i] = 2*images[i]->stride[0]; images_pt[i] = THTensor_(data)(images[i]); } } for(i=0;i<npixels;i++){ cImgOff = (long unsigned int)*offImg - 1; curImg_pt = images_pt[cImgOff]; if ((*offIndexXY > 0) && (*offIndexXY < images_npixels[cImgOff])){ XYoffset = (long unsigned int)*offIndexXY; *panoR = curImg_pt[XYoffset]; *panoG = curImg_pt[XYoffset + images_Goff[cImgOff]] ; *panoB = curImg_pt[XYoffset + images_Boff[cImgOff]]; } panoR++; panoG++; panoB++; offImg++; offIndexXY++; } return 0; }