Lua logo

Preface

This is the reference manual of MoonImage, which is a Lua image loading library based on Sean Barrett’s STB libraries. [1]

It is assumed that the reader is familiar with the Lua programming language.

For convenience of reference, this document contains external (deep) links to the Lua Reference Manual.

Getting and installing

For installation intructions, refer to the README file in the MoonImage official repository on GitHub.

Module organization

The MoonImage module is loaded using Lua’s require() and returns a table containing the functions it provides (as usual with Lua modules). This manual assumes that such table is named mi, i.e. that it is loaded with:

 mi = require("image")

but nothing forbids the use of a different name.

Examples

Complete examples can be found in the examples/ directory of the release package.

License

MoonImage is released under the MIT/X11 license (same as Lua, and with the same only requirement to give proper credits to the original author). The copyright notice is in the LICENSE file in the base directory of the official repository on GitHub.

See also

MoonImage is part of MoonLibs, a collection of Lua libraries for graphics and audio programming.

Load images

  • data, w, h, channels = load(filename, [desiredchannels], [chantype])
    Loads an image from a file, and returns the image data, its width w and height h, and the color channels contained in data (which is the same as desiredchannels unless this is nil, in which case channels is the same as in the image file).
    The returned data is a binary string containing w*h pixels, each pixel consisting of up to 4 interleaved components (one per channel).
    The type of the components is determined by the chantype parameter:
    -if chantype is 'u8' or nil, then each component is an unsigned 8 bit integer;
    -if chantype is 'u16', then each component is an unsigned 16 bit integer;
    -if chantype is 'f', then each component is a single precision float value.
    Note that this may involve lossy data conversion if chantype does not match how data is stored in the image file.
    (u16 values are converted to u8 by just keeping the MSB, u8 values are converted to u16 by mapping x to x*28+x, while conversion to or from floats is done using gamma correction with the current gamma and scale values.)

  • w, h, channels = info(filename)
    Retrieves information for an image without loading it.

  • boolean = is_hdr(filename)
    Returns true the image contained in filename is HDR (High Dynamic Range).

  • set_gamma_and_scale(gamma, scale)
    gamma, scale = gamma_and_scale( )
    Set/get the gamma and scale values used in gamma correction (by default, gamma = 2.2 and scale = 1.0).

  • flip_vertically_on_load(boolean)
    unpremultiply_on_load(boolean)
    convert_iphone_png_to_rgb(boolean)
    Load flags that control a few aspects of how an image is loaded (see stb_image.h for more details).

  • reset_load_flags( )
    Resets all the load flags to false (which is also their initial value).

  • data = reduce_to_u8(data, w, h, channels, chantype)
    Downgrades 'u16' or 'f' image data to 'u8' (see load( )).

Write images

  • write_png(filename, w, h, channels, data, [stride])
    Write data to a PNG file. Expects w*h*#channels bytes of data.

  • write_bmp(filename, w, h, channels, data)
    Write data to a BMP file. Expects w*h*#channels bytes of data.

  • write_tga(filename, w, h, channels, data, [rle])
    Write data to a TGA file. Expects w*h*#channels bytes of data.
    Pass rle = true to use RLE compression.

  • write_jpg(filename, w, h, channels, data, quality)
    Write data to a JPG file. Expects w*h*#channels bytes of data.
    quality = 1 (min) .. 100 (max).

  • write_hdr(filename, w, h, channels, data)
    Write data to a HDR file. Expects w*h*#channels*sizeof(float) bytes of data.

Perlin noise

  • value = perlin(x, [y, z, xwrap, ywrap, zwrap, seed])
    Computes a random value at coordinates (x, y, z).
    Binding to stb_perlin_noise3_seed( ). See stb_perlin.h for details.

Enums

channels: 'y' (grey), 'ya' (grey alpha), 'rgb', 'rgba'.
Notice that #channels gives the number of channels (e.g. #'rgb' = 3 channels).

chantype: 'u8', 'u16', 'f'.


1. This manual is written in AsciiDoc, rendered with AsciiDoctor and a CSS from the AsciiDoctor Stylesheet Factory.