Lua Binding

Overview

All the IM functions are available in Lua, with a few exceptions. We call it ImLua. To use them the general application will do require"imlua", and require"imluaxxxx" to all other secondary libraries that are needed. The functions and definitions will be available under the table "im" using the following name rules:

imXxx  -> im.Xxx    (for functions)
IM_XXX -> im.XXX (for definitions)
imFileXXX(ifile,... -> ifile:XXX(... (for methods) imImageXXX(image,... -> image:XXX(... (for methods)

New functions (without equivalents in C) were implemented to create and destroy objects that do not exist in C. For instance functions were developed to create and destroy palettes. All the metatables have the "tostring" metamethod implemented to help debuging. The imImage metatable has the "index" metamethod so you can address its data directly in Lua. Some functions were modified to receive those objects as parameters.

Also the functions which receive values by reference in C were modified. Generally, the values of parameters that would have their values modified are now returned by the function in the same order.

Notice that, as opposed to C, in which enumeration flags are combined with the bitwise operator OR, in Lua the flags are added arithmetically.

In Lua all parameters are checked and a Lua error is emitted when the check fails.

All the objects are garbage collected by the Lua garbage collector.

Initialization

Lua 5.1 "require" can be used for all the ImLua libraries. You can use require"imlua" and so on, but the LUA_CPATH must also contains the following:

"./lib?51.so;"    [in UNIX]

".\\?51.dll;"     [in Windows]

Also compatible with Lua 5.2 and 5.3, just replace the "51" suffix by "52" or "53".

The LuaBinaries distribution already includes these modifications on the default search path.

If you are using another Lua distribution you can use the environment:

export LUA_CPATH=./\?.so\;./lib\?.so\;./lib\?51.so\;   [in UNIX]

Or you can set it in Lua before loading iup modules:

package.cpath = package.cpath .. "./lib?51.so;"    [in UNIX]

package.cpath = package.cpath .. ".\\?51.dll"     [in Windows]

The simplest form require"im" and so on, can not be used because there are IM dynamic libraries with names that will conflict with the names used by require during search.

Additionally you can statically link the ImLua libraries, but you must call the initialization functions manually. The imlua_open function is declared in the header file imlua.h, see the example below:

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <imlua.h>
void main(void)
{
  lua_State *L = lua_open();

  luaopen_string(L);
  luaopen_math(L);
  luaopen_io(L);

  imlua_open(L);

  lua_dofile("myprog.lua");

  lua_close(L);
}

Calling imlua_close is optional. In Lua it can be called using "im.Close()". It can be used to avoid a memory leak. See imFormatRemoveAll in File Formats. (since 3.9.1)

imImage Usage

imImage structure members are accessed using member functions in Lua. For instance:

In C   In Lua
image->width   image:Width()
image->height   image:Height()
image->color_space   image:ColorSpace()
image->data_type   image:DataType()
image->has_alpha   image:HasAlpha()
image->depth   image:Depth()

Data can also be accessed in Lua in two different ways.

First, using data indexing with plane, line and column. image[plane] returns an object that represents an image plane. image[plane][line] returns an object that represents an image line of that plane. And finally image[plane][line][column] returns an object that represents the pixel value of the column in the line of that plane. All indices use the same start as in C, i.e. all start at 0. Only the pixel value can has its value changed. When data_type is IM_CFLOAT then value is a table with two numbers.

Second, all pixels can be changed or retrieved at once using the "image:SetPixels(table)" and "table = image:GetPixels()" member functions. The number of elements in the table must be equal to "width * height * depth". If there is alpha the depth must be incremented by 1. If data_type is IM_CFLOAT depth must be duplicated. Data organization is the same as in the image->data member. The table indices starts at 1.  (Since 3.9)

Integration with CDLua

In CDLua there is an additional library providing simple functions to map the imImage structure to the cdBitmap structure. And some facilities to draw an image in a CD canvas. See also the CD documentation and the IM Lua 5 Binding reference.

Color values and palettes can be created and used transparently in both libraries. Palettes and color values are 100% compatible between CD and IM.

Reference

See also the ImLua 5 Binding Reference.