View Source StbImage (StbImage v0.2.0)

Tiny image encoding and decoding.

The following formats are supported:

  • JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
  • PNG 1/2/4/8/16-bit-per-channel
  • TGA
  • BMP non-1bpp, non-RLE
  • PSD (composited view only, no extra channels, 8/16 bit-per-channel)
  • GIF (always reports as 4-channel)
  • HDR (radiance rgbE format)
  • PIC (Softimage PIC)
  • PNM (PPM and PGM binary only)

There are also specific functions for working with GIFs.

Link to this section Summary

Functions

The StbImage struct.

Decodes image from binary.

Decodes image from file at path.

Decodes GIF image from binary.

Decodes GIF image from file at path.

Encodes image to a binary.

Saves image to the file at path.

Link to this section Functions

The StbImage struct.

It has the following fields:

  • :data - a binary
  • :shape - a tuple with the {height, width, channels}
  • :type - the type unit in the binary (u8/u16/f32)
  • :color_mode - the color mode as :l, :la, :rgb, or :rgba
Link to this function

from_binary(buffer, opts \\ [])

View Source

Decodes image from binary.

options

Options

  • :channels - The number of desired channels. Use 0 for auto-detection. Defaults to 0.

  • :type - The type of the data. Defaults to :u8. Must be one of :u8, :u16, :f32.

example

Example

{:ok, buffer} = File.read("/path/to/image")
img = StbImage.from_binary(buffer)
{h, w, c} = img.shape
img = img.data

# If you know the image is a 4-channel image and auto-detection failed
img = StbImage.from_file("/path/to/image", channels: 4)
{h, w, c} = img.shape
img = img.data
Link to this function

from_file(path, opts \\ [])

View Source

Decodes image from file at path.

options

Options

  • :channels - The number of desired channels. Use 0 for auto-detection. Defaults to 0.

  • :type - The type of the data. Defaults to :u8. Must be one of :u8, :u16, :f32.

example

Example

img = StbImage.from_file("/path/to/image")
{h, w, c} = img.shape
data = img.data

# If you know the image is a 4-channel image and auto-detection failed
img = StbImage.from_file("/path/to/image", channels: 4)
{h, w, c} = img.shape
img = img.data

Decodes GIF image from binary.

example

Example

{:ok, buffer} = File.read("/path/to/image")
{:ok, frames, delays} = StbImage.gif_from_binary(buffer)
frame = Enum.at(frames, 0)
{h, w, 3} = frame.shape

# GIFs always have channels == :rgb and type == :u8
# delays is a list that has n elements, where n is the number of frames

Decodes GIF image from file at path.

example

Example

{:ok, frames, delays} = StbImage.gif_from_file("/path/to/image")
frame = Enum.at(frames, 0)
{h, w, 3} = frame.shape

# GIFs always have channels == :rgb and type == :u8
# delays is a list that has n elements, where n is the number of frames
Link to this function

to_binary(stb_image, format)

View Source

Encodes image to a binary.

The supported formats are :jpg, :png, :bmp, :tga.

example

Example

{:ok, binary} = StbImage.to_binary(:png, img, height, width, channels)
Link to this function

to_file(stb_image, path, opts \\ [])

View Source

Saves image to the file at path.

The supported formats are :jpg, :png, :bmp, :tga.

The format is determined from the file extension if possible, you can also pass it explicitly via the :format option.

Returns :ok on success and {:error, reason} otherwise.

Make sure the directory you intent to write the file to exists, otherwise an error is returned.

options

Options

  • :format - one of the supported image formats