Packages
vix
0.18.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.1
0.33.0
0.32.0
0.31.1
0.31.0
0.30.0
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.1
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.0
0.7.0
0.6.1
0.6.0
0.5.0
0.4.0
0.3.0
0.2.1
0.2.0
retired
0.1.0
NIF based bindings for libvips
Current section
Files
Jump to
Current section
Files
lib/vix/tensor.ex
defmodule Vix.Tensor do
alias Vix.Vips.Image
@moduledoc """
Struct to hold raw pixel data returned by the libvips along with metadata about the binary.
Useful for interoperability between other libraries like [Nx](https://hexdocs.pm/nx/Nx.html), [Evision](https://github.com/cocoa-xu/evision/).
See `Vix.Vips.Image.write_to_tensor/1` to convert an vix image to tensor.
"""
@typedoc """
Type of the image pixel when image is represented as Tensor.
This type is useful for interoperability between different libraries. Type value is same as [`Nx.Type.t()`](https://hexdocs.pm/nx/Nx.Type.html)
"""
@type tensor_type() ::
{:u, 8}
| {:s, 8}
| {:u, 16}
| {:s, 16}
| {:u, 32}
| {:s, 32}
| {:f, 32}
| {:f, 64}
@typedoc """
Struct to hold raw pixel data returned by the Libvips along with metadata about the binary.
`:names` will always be `[:width, :height, :bands]`
"""
@type t() :: %__MODULE__{
data: binary(),
shape: {non_neg_integer(), non_neg_integer(), non_neg_integer()},
names: list(),
type: tensor_type()
}
defstruct data: nil,
shape: {0, 0, 0},
names: [:width, :height, :bands],
type: {}
@doc """
Convert Vix image pixel format to [Nx tensor type](https://hexdocs.pm/nx/Nx.Type.html#t:t/0)
Vix internally uses [libvips image
format](https://www.libvips.org/API/current/VipsImage.html#VipsBandFormat). To
ease the interoperability between Vix and other elixir libraries, we
can use this function.
"""
@spec type(image :: Image.t()) :: tensor_type() | no_return()
def type(image) do
# TODO: should we support :VIPS_FORMAT_COMPLEX and :VIPS_FORMAT_DPCOMPLEX ?
image |> Image.format() |> nx_type()
end
defp nx_type(:VIPS_FORMAT_UCHAR), do: {:u, 8}
defp nx_type(:VIPS_FORMAT_CHAR), do: {:s, 8}
defp nx_type(:VIPS_FORMAT_USHORT), do: {:u, 16}
defp nx_type(:VIPS_FORMAT_SHORT), do: {:s, 16}
defp nx_type(:VIPS_FORMAT_UINT), do: {:u, 32}
defp nx_type(:VIPS_FORMAT_INT), do: {:s, 32}
defp nx_type(:VIPS_FORMAT_FLOAT), do: {:f, 32}
defp nx_type(:VIPS_FORMAT_DOUBLE), do: {:f, 64}
defp nx_type(other),
do: raise(ArgumentError, "Cannot convert this image type to binary. Found #{inspect(other)}")
end