Pngex (pngex v0.1.2)

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

@type 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()

@type 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
@type data() :: binary() | [pos_integer()] | [rgb_color()] | [rgba_color()]

Image data.

rgb

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

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

Grayscale

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

grayscale-and-alpha

Grayscale and Alpha

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

indexed

Indexed

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

depth-of-binary

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), ...>>
@type pos_int32() :: 1..4_294_967_295

Positive 32-bit integer.

It's for width and height.

@type 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()

@type 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()

@type 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

@type 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)

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

Generates a PNG image.

examples

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 \\ [])

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

Creates a new Pngex structure.

options

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

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)

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

Sets bit depth.

examples

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)

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

Sets image hieght.

examples

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)

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

Sets a palette.

examples

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)

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

Sets image width and height.

examples

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)

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

Sets color type.

examples

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)

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

Sets image width.

examples

Examples

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