Pngex (pngex v0.1.1)

Generates PNG images.

Link to this section Summary

Types

Bit depth.

PNG color type.

Image data.

Positive 32-bit integer.

Data type of RGB color.

Data type of RGB color and alpha.

Type of filtering.

t()

Configurations for PNG image.

Functions

Generates a PNG image.

Creates a new Pngex structure.

Sets bit depth.

Sets image hieght.

Sets a palette.

Sets image width and height.

Sets color type.

Sets image width.

Link to this section Types

Specs

bit_depth() :: :depth1 | :depth2 | :depth4 | :depth8 | :depth16

Bit depth.

  • :depth1 - 1 bits
  • :depth2 - 2 bits
  • :depth4 - 4 bits
  • :depth8 - 8 bits
  • :depth16 - 16 bits
Link to this type

color_type()

Specs

color_type() :: :gray | :rgb | :indexed | :gray_and_alpha | :rgba

PNG color type.

  • :gray - grayscale
  • :rgb - RGB color
  • :indexed - palette color
  • :gray_and_alpha - grayscale and alpha
  • :rgba - RGB color and alpha

Specs

data() :: binary() | [pos_integer()] | [rgb_color()] | [rgba_color()]

Image data.

RGB

typeexample
binary<<r0, g0, b0, r1, g1, b1, ...>>
list of integers[r0, g0, b0, r1, g1, b1, ...]
list of tuples[{r0, g0, b0}, {r1, g1, b1}, ...]

RGB and Alpha

typeexample
binary<<r0, g0, b0, a0, r1, g1, b1, a1, ...>>
list of integers[r0, g0, b0, a0, r1, g1, b1, a1, ...]
list of tuples[{r0, g0, b0, a0}, {r1, g1, b1, a1}, ...]

Grayscale

typeexample
binary<<c0, c1, ...>>
list of integers[c0, c1, ...]

Grayscale and Alpha

typeexample
binary<<c0, a0, c1, a1, ...>>
list of integers[c0, a0, c1, a1, ...]

Indexed

typeexample
binary<<c0, c1, ...>>
list of integers[c0, c1, ...]

Depth of binary

If you use binaries, you may need to use size options.

depthexample
:depth1<<c0::size(1), c1::size(1), ...>>
:depth2<<c0::size(2), c1::size(2), ...>>
:depth4<<c0::size(4), c1::size(4), ...>>
:depth8<<c0, c1, ...>> or <<c0::size(8), c1::size(8), ...>>
:depth16<<c0::size(16), c1::size(16), ...>>

Specs

pos_int32() :: 1..4_294_967_295

Positive 32-bit integer.

It's for width and height.

Specs

rgb_color() :: {r :: pos_integer(), g :: pos_integer(), b :: pos_integer()}

Data type of RGB color.

A tuple of red, green and blue values.

Link to this type

rgba_color()

Specs

rgba_color() ::
  {r :: pos_integer(), g :: pos_integer(), b :: pos_integer(),
   a :: pos_integer()}

Data type of RGB color and alpha.

A tuple of red, green, blue and alpha values.

Link to this type

scanline_filter()

Specs

scanline_filter() :: 0 | 1 | 2 | 3 | 4

Type of filtering.

  • 0 - None
  • 1 - Sub
  • 2 - Up
  • 3 - Average
  • 4 - Paeth

see: https://en.wikipedia.org/wiki/Portable_Network_Graphics#Filtering

Specs

t() :: %Pngex{
  depth: bit_depth(),
  height: pos_int32(),
  palette: [rgb_color()],
  scanline_filter: scanline_filter(),
  type: color_type(),
  width: pos_int32()
}

Configurations for PNG image.

Link to this section Functions

Link to this function

generate(pngex, data)

Specs

generate(t(), data()) :: iolist()

Generates a PNG image.

Examples

image =
  Pngex.new(type: :rgb, depth: :depth8, width: 16, height: 16)
  |> Pngex.generate(for(c <- 0..255, do: {c, 255 - c, 0}))

File.write("image.png", image)
Link to this function

new(opts \\ [])

Specs

new(keyword()) :: t() | {:error, keyword()}

Creates a new Pngex structure.

Options

  • :type - color type;
    • :gray - grayscale
    • :rgb - RGB (default)
    • :indexed - palette color
    • :gray_and_alpha - grayscale and alpha
    • :rgba - RGB and alpha
  • :depth - color depth; :depth2, :depth4, :depth8 (default) or :depth16
  • :width - image width; 32-bit integer (1..4,294,967,295)
  • :height - image height; 32-bit integer (1..4,294,967,295)
  • :palette - palette table; list of RGB color tuples

Examples

pngex =
  Pngex.new(
    type: :indexed,
    depth: :depth8,
    width: 640,
    height: 480,
    palette: [{0, 0, 0}, {255, 255, 255}]
  )
Link to this function

set_depth(pngex, depth)

Specs

set_depth(t(), bit_depth()) :: t() | {:error, [{:invalid_depth, any()}]}

Sets bit depth.

Examples

iex> Pngex.new() |> Pngex.set_depth(:depth16)
%Pngex{depth: :depth16}
iex> Pngex.new() |> Pngex.set_depth(:depth15)
{:error, invalid_depth: :depth15}
Link to this function

set_height(pngex, height)

Specs

set_height(t(), pos_int32()) :: t() | {:error, [{:invalid_height, any()}]}

Sets image hieght.

Examples

iex> Pngex.new() |> Pngex.set_height(128)
%Pngex{height: 128}
iex> Pngex.new() |> Pngex.set_height(0)
{:error, invalid_height: 0}
Link to this function

set_palette(pngex, palette)

Specs

set_palette(t(), [rgb_color()]) :: t() | {:error, [{:invalid_palette, any()}]}

Sets a palette.

Examples

iex> Pngex.new() |> Pngex.set_palette([{0, 0, 0}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}])
%Pngex{palette: [{0, 0, 0}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}]}
iex> Pngex.new() |> Pngex.set_palette([0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255])
{:error, invalid_palette: [0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255]}
Link to this function

set_size(pngex, width, height)

Specs

set_size(t(), pos_int32(), pos_int32()) ::
  t() | {:error, [{:invalid_size, map()}]}

Sets image width and height.

Examples

iex> Pngex.new() |> Pngex.set_size(640, 480)
%Pngex{width: 640, height: 480}
iex> Pngex.new() |> Pngex.set_size(0, 480)
{:error, invalid_size: %{width: 0}}
Link to this function

set_type(pngex, type)

Specs

set_type(t(), color_type()) :: t() | {:error, [{:invalid_type, any()}]}

Sets color type.

Examples

iex> Pngex.new() |> Pngex.set_type(:gray)
%Pngex{type: :gray}
iex> Pngex.new() |> Pngex.set_type(:monotone)
{:error, invalid_type: :monotone}
Link to this function

set_width(pngex, width)

Specs

set_width(t(), pos_int32()) :: t() | {:error, [{:invalid_width, any()}]}

Sets image width.

Examples

iex> Pngex.new() |> Pngex.set_width(128)
%Pngex{width: 128}
iex> Pngex.new() |> Pngex.set_width(0)
{:error, invalid_width: 0}