static int lcairo_create_image(lua_State *L){ int w=luaL_checkint(L,1); int h=luaL_checkint(L,2); tImage *p=(tImage*)luaT_alloc(L, sizeof(tImage)); p->data = luaT_alloc(L,w*h*4); memset(p->data,0xff,w*h*4); p->surf = cairo_image_surface_create_for_data(p->data,CAIRO_FORMAT_ARGB32,w,h,4*w); luaT_pushudata(L, p, tImage_id); return 1; }
static int torch_DiskFile_new(lua_State *L) { const char *name = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); int isQuiet = luaT_optboolean(L, 3, 0); int isReadable; int isWritable; FILE *handle; DiskFile *file; luaL_argcheck(L, torch_DiskFile_c_mode(mode, &isReadable, &isWritable), 2, "file mode should be 'r','w' or 'rw'"); if( isReadable && isWritable ) { handle = fopen(name, "r+b"); if(!handle) { handle = fopen(name, "wb"); if(handle) { fclose(handle); handle = fopen(name, "r+b"); } } } else handle = fopen(name, (isReadable ? "rb" : "wb")); if(!handle) { if(isQuiet) return 0; else luaL_error(L, "cannot open <%s> in mode %c%c", name, (isReadable ? 'r' : ' '), (isWritable ? 'w' : ' ')); } file = luaT_alloc(L, sizeof(DiskFile)); file->handle = handle; file->flags.isQuiet = isQuiet; file->flags.isReadable = isReadable; file->flags.isWritable = isWritable; file->isNativeEncoding = 1; file->flags.isBinary = 0; file->flags.isAutoSpacing = 1; file->flags.hasError = 0; file->name = luaT_alloc(L, strlen(name)+1); strcpy(file->name, name); luaT_pushudata(L, file, torch_DiskFile_id); return 1; }
static int cutorch_Event_new(lua_State *L) { cudaEvent_t *event = luaT_alloc(L, sizeof(cudaEvent_t)); THCudaCheck(cudaEventCreate(event)); THCState *state = cutorch_getstate(L); THCudaCheck(cudaEventRecord(*event, THCState_getCurrentStream(state))); luaT_pushudata(L, event, "cutorch.Event"); return 1; }
static int pa_openstream(lua_State *L) { double samplerate = 0; unsigned long nbufframe = 0; pa_Stream *stream = NULL; int narg = lua_gettop(L); int hascallback = 0; PaStreamParameters inparams; PaStreamParameters outparams; PaStreamFlags flags = 0; if((narg == 5 || (narg == 6 && lua_isfunction(L, 6))) && (lua_istable(L, 1) || lua_isnil(L, 1)) && (lua_istable(L, 2) || lua_isnil(L, 2)) && lua_isnumber(L, 3) && lua_isnumber(L, 4) && lua_isnumber(L, 5)) { if(lua_istable(L, 1)) pa_readstreamparameters(L, 1, &inparams); if(lua_istable(L, 2)) pa_readstreamparameters(L, 2, &outparams); samplerate = (double)lua_tonumber(L, 3); nbufframe = (unsigned long)lua_tonumber(L, 4); flags = (PaStreamFlags)lua_tonumber(L, 5); if(narg == 6) hascallback = 1; } else luaL_error(L, "expected arguments: (table | nil) (table | nil) number number number [function]"); stream = luaT_alloc(L, sizeof(pa_Stream)); stream->id = NULL; stream->ninchannel = (lua_istable(L, 1) ? inparams.channelCount : 0); stream->noutchannel = (lua_istable(L, 2) ? outparams.channelCount : 0); stream->insampleformat = (lua_istable(L, 1) ? inparams.sampleFormat : 0); stream->outsampleformat = (lua_istable(L, 2) ? outparams.sampleFormat : 0); if(!(stream->pa_L = luaL_newstate())) luaL_error(L, "could not allocate new state"); stream->callbackerror = NULL; luaT_pushudata(L, stream, "pa.Stream"); luaL_openlibs(stream->pa_L); if(hascallback) pa_setcallback__(L, 6, stream); pa_checkerror(L, Pa_OpenStream(&stream->id, (lua_istable(L, 1) ? &inparams : NULL), (lua_istable(L, 2) ? &outparams : NULL), samplerate, nbufframe, flags, (hascallback ? streamcallbackshort : NULL), (hascallback ? stream : NULL))); return 1; }
/* Image */ static int lcairo_create_image_from_png(lua_State *L){ const char *filename=luaL_checkstring(L,1); tImage *p=(tImage*)luaT_alloc(L, sizeof(tImage)); p->data = NULL; /* won't have this */ p->surf = cairo_image_surface_create_from_png(filename); if (cairo_surface_status(p->surf)!=CAIRO_STATUS_SUCCESS){ printf("image_create_from_png: ERROR: couldn't load %s\n",filename); cairo_surface_destroy(p->surf); return 0; // lua_settop(L,1); // return -1; } luaT_pushudata(L, p, tImage_id); return 1; }
static int pa_opendefaultstream(lua_State *L) { int numinchan = 0; int numoutchan = 0; PaSampleFormat sampleformat = 0; double samplerate = 0; unsigned long nbufframe = 0; pa_Stream *stream = NULL; int narg = lua_gettop(L); int hascallback = 0; if((narg == 5 || (narg == 6 && lua_isfunction(L, 6))) && lua_isnumber(L, 1) && lua_isnumber(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4) && lua_isnumber(L, 5)) { numinchan = (int)lua_tonumber(L, 1); numoutchan = (int)lua_tonumber(L, 2); sampleformat = (PaSampleFormat)lua_tonumber(L, 3); samplerate = (double)lua_tonumber(L, 4); nbufframe = (unsigned long)lua_tonumber(L, 5); if(narg == 6) hascallback = 1; } else luaL_error(L, "expected arguments: number number number number number [function]"); stream = luaT_alloc(L, sizeof(pa_Stream)); stream->id = NULL; stream->ninchannel = numinchan; stream->noutchannel = numoutchan; stream->insampleformat = sampleformat; stream->outsampleformat = sampleformat; if(!(stream->pa_L = luaL_newstate())) luaL_error(L, "could not allocate new state"); stream->callbackerror = NULL; luaT_pushudata(L, stream, "pa.Stream"); luaL_openlibs(stream->pa_L); if(hascallback) pa_setcallback__(L, 6, stream); pa_checkerror(L, Pa_OpenDefaultStream(&stream->id, numinchan, numoutchan, sampleformat, samplerate, nbufframe, (hascallback ? streamcallbackshort : NULL), (hascallback ? stream : NULL))); return 1; }
void* luaT_realloc(lua_State *L, void *ptr, long size) { if(!ptr) return(luaT_alloc(L, size)); if(size == 0) { luaT_free(L, ptr); return NULL; } if(size < 0) luaL_error(L, "$ Torch: invalid memory size -- maybe an overflow?"); ptr = realloc(ptr, size); if(!ptr) luaL_error(L, "$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824); return ptr; }