lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I think I'm right to assume that you want to change

void process_data(t_mem_header *memory_in_header, char *memory_in,
t_mem_header *memory_out_header, char *memory_out)
        leaving out stuff like variable declarations

      for(j=0; j < height; j++) {
            for(i=0; i < width; i++) {
                  //process input and put result in output

to something like (pseudocode):

void process_data(t_mem_header *memory_in_header, char *memory_in,
t_mem_header *memory_out_header, char *memory_out)
lua_State * L; // either retrieve L from global or create a new L & openlibs etc
	// load the libraries for dealing with t_mem_header data
int err = lua_loadfile/loadstring(L, ...) // load the lua script if necessary

	// get a lua function to call from the script
	lua_getglobal(L, "process");

	// put the arguments into lua
	lua_pushlightuserdata(L, memory_in_header);
	lua_pushlightuserdata(L, memory_in);
	lua_pushlightuserdata(L, memory_out_header);
	lua_pushlightuserdata(L, memory_out);

	// call the 'process' function from the lua script
	int err = lua_pcall(L, 5, 0, 0);

	lua_settop(L, 0);	// clean up if you're going to re-use L

The lua script would need to have a function something like this:

function process(in_header, in_data, out_header, out_data)
	-- call library methods passing the userdata as arguments

The Lua state would need some functions defined for working with the userdata types, e.g. width(in_header).


You might however want to make this a little more encapsulated, whether by keeping the header/data separation hidden in Lua and just calling process(in, out), or by associating type-checking with the lightuserdata using the registry or environment. Probably the best thing is to write out a few typical examples of what you would want the scripts to look like, then we can figure out how to make that work.


On Sep 26, 2006, at 6:18 AM, varol kaptan wrote:

the luaglut library
( has a
component called memarray which may be used to create pools of raw
memory and view them as uchar, char, int, double, etc. Instead of
creating new memory you can also provide alternative views to an
already allocated memory by providing the starting address as a light

Some examples:

varol@thales:~$ lua
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
require 'memarray'
a = memarray('int', 8)
=a:size(), a:length()
32	8
for i = 0, a:length()-1 do a[i] = i end
for i = 0, a:length()-1 do io.write(a[i], ' ') end print()
0 1 2 3 4 5 6 7
b = memarray('uint', 4, a:ptr(4)) -- just a different view of a, starting from 4'th integer
for i = 0, b:length()-1 do io.write(b[i], ' ') end print()
4 5 6 7
=a[4], b[0]
4	4
a[4] = -1
=a[4], b[0]
-1	4294967295

Varol Kaptan

On 9/26/06, Wesley Smith <> wrote:
I'm wanting to use Lua's Light User Data functionality to prototype
some data processing routines.  I'm basically dealing with a pointer
to raw memory and a header struct that describes the memory layout.
It looks something like this in C:

char *memory_in;
t_mem_header *memory_in_header;

char *memory_out;
t_mem_header *memory_out_header;

What I want to do is pass all 4 of these pointers to lua in as light
user data.  Allocation and deallocation of this memory is handled by
the app.  Typical C usage of this goes as follow:

void process_data(t_mem_header *memory_in_header, char *memory_in,
t_mem_header *memory_out_header, char *memory_out)
        leaving out stuff like variable declarations

      for(j=0; j < height; j++) {
            for(i=0; i < width; i++) {
                  //process input and put result in output

Now, the data can be many flavors like uchar, long, float, or double.
I'd like to use Lua to replace the double for loops such that I can
load little processing scripts and see how things work and when I get
something that works well I would then most likely go back to C and
hard code it. A secondary motive for doing this is to get to know Lua
more and just try some ideas out.  I've looked around for some info
and examples on uses of Light User Data, but I'm still not sure
exactly where to start.  How do I go about writing Lua functions that
process the Light User Data such that I can script the processing?

As a simple starter, how would I recreate the following:
 *(float *)(memory_out + j*width + i) =  1.0 + *(float *)(memory_in+
j*width + i);