Blendend.Image (blendend v0.3.0)

View Source

Image loading functions for Blendend.

This module deals with image resources that can be used anywhere pixel data is needed: as sources for patterns, or passed directly to canvas blit calls.

Typical uses:

Summary

Types

t()

Opaque image resource (pixel buffer). Load via from_file!/1 or from_data/1.

Functions

Returns a blurred copy of image using a Gaussian approximation.

Same as blur/2, but returns the blurred image or raises on failure.

Decodes a QOI binary into raw RGBA bytes for tests and diagnostics.

Loads an image from an in-memory binary.

Loads image data from a binary and converts it to A8 (alpha-only) using the given channel.

Loads an image from path.

Same as from_file/1, but returns the image directly.

Loads an image from path and converts it to an 8-bit mask using the given channel.

Same as from_file_a8/2, but raises on failure.

Returns the image size in pixels.

Same as size/1, but returns the {width, height} tuple directly.

Types

t()

@opaque t()

Opaque image resource (pixel buffer). Load via from_file!/1 or from_data/1.

Functions

blur(image, sigma)

@spec blur(t(), number()) :: {:ok, t()} | {:error, term()}

Returns a blurred copy of image using a Gaussian approximation.

sigma controls blur strength in pixels (roughly 3×sigma is the visible radius). Supports PRGB32 and A8 images; other formats are converted to PRGB32 first.

blur!(image, sigma)

@spec blur!(t(), number()) :: t()

Same as blur/2, but returns the blurred image or raises on failure.

decode_qoi(bin)

@spec decode_qoi(binary()) ::
  {:ok, {pos_integer(), pos_integer(), binary()}} | {:error, term()}

Decodes a QOI binary into raw RGBA bytes for tests and diagnostics.

Returns {:ok, {width, height, data}} where data is a binary of length width * height * 4 in RGBA byte order.

decode_qoi!(bin)

@spec decode_qoi!(binary()) :: {pos_integer(), pos_integer(), binary()}

from_data(bin)

@spec from_data(binary()) :: {:ok, t()} | {:error, term()}

Loads an image from an in-memory binary.

The binary is the raw image file contents.

On success, returns {:ok, image}.

On failure, returns {:error, reason}.

from_data_a8(bin, channel \\ :red)

@spec from_data_a8(binary(), atom()) :: {:ok, t()} | {:error, term()}

Loads image data from a binary and converts it to A8 (alpha-only) using the given channel.

from_file(path)

@spec from_file(String.t()) :: {:ok, t()} | {:error, term()}

Loads an image from path.

This reads the file in Elixir and lets the NIF decode the bytes. The file must be in a format blend2d understands (e.g. PNG, JPEG, BMP, QOI ).

On success, returns {:ok, image} where image is an opaque resource.

On failure, returns {:error, reason}.

from_file!(path)

@spec from_file!(String.t()) :: t()

Same as from_file/1, but returns the image directly.

On success, returns image.

On failure, raises Blendend.Error.

from_file_a8(path, channel \\ :red)

@spec from_file_a8(String.t(), atom()) :: {:ok, t()} | {:error, term()}

Loads an image from path and converts it to an 8-bit mask using the given channel.

Channel can be :red (default), :green, :blue, :alpha, or :luma.

from_file_a8!(path, channel \\ :red)

@spec from_file_a8!(String.t(), atom()) :: t()

Same as from_file_a8/2, but raises on failure.

size(image)

@spec size(t()) :: {:ok, {non_neg_integer(), non_neg_integer()}} | {:error, term()}

Returns the image size in pixels.

On success, returns {:ok, {width, height}} where dimensions are in pixels.

On failure, returns {:error, reason}.

size!(image)

@spec size!(t()) :: {non_neg_integer(), non_neg_integer()}

Same as size/1, but returns the {width, height} tuple directly.

On success, returns {width, height}.

On failure, raises Blendend.Error.