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.
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
bit_depth()
@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
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
data()
@type data() :: binary() | [pos_integer()] | [rgb_color()] | [rgba_color()]
Image data.
rgb
RGB
type | example |
---|---|
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
type | example |
---|---|
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
type | example |
---|---|
binary | <<c0, c1, ...>> |
list of integers | [c0, c1, ...] |
grayscale-and-alpha
Grayscale and Alpha
type | example |
---|---|
binary | <<c0, a0, c1, a1, ...>> |
list of integers | [c0, a0, c1, a1, ...] |
indexed
Indexed
type | example |
---|---|
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.
depth | example |
---|---|
: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), ...>> |
pos_int32()
@type pos_int32() :: 1..4_294_967_295
Positive 32-bit integer.
It's for width and height.
rgb_color()
@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.
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.
scanline_filter()
@type scanline_filter() :: 0 | 1 | 2 | 3 | 4
Type of filtering.
0
- None1
- Sub2
- Up3
- Average4
- 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
generate(pngex, data)
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)
new(opts \\ [])
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}]
)
set_depth(pngex, depth)
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}
set_height(pngex, height)
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}
set_palette(pngex, palette)
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]}
set_size(pngex, width, height)
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}}
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}
set_width(pngex, width)
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}