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]

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

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);
}

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.