-
Notifications
You must be signed in to change notification settings - Fork 17
/
interpolate.ex
103 lines (78 loc) · 1.98 KB
/
interpolate.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
defmodule Vix.Vips.Interpolate do
alias Vix.Type
defstruct [:ref]
alias __MODULE__
@moduledoc """
Make interpolators for operators like `affine` and `mapim`.
"""
alias Vix.Type
alias Vix.Nif
@behaviour Type
@typedoc """
Represents an instance of VipsInterpolate
"""
@type t() :: %Interpolate{ref: reference()}
@impl Type
def typespec do
quote do
unquote(__MODULE__).t()
end
end
@impl Type
def default(nil), do: :unsupported
@impl Type
def to_nif_term(interpolate, _data) do
case interpolate do
%Interpolate{ref: ref} ->
ref
value ->
raise ArgumentError, message: "expected Vix.Vips.Interpolate. given: #{inspect(value)}"
end
end
@impl Type
def to_erl_term(ref), do: %Interpolate{ref: ref}
@doc """
Make a new interpolator by name.
Make a new interpolator from the libvips class nickname. For example:
```elixir
{:ok, interpolate} = Interpolate.new("bilindear")
```
You can get a list of all supported interpolators from the command-line with:
```shell
$ vips -l interpolate
```
See for example `affine`.
"""
@spec new(String.t()) :: {:ok, __MODULE__.t()} | {:error, term()}
def new(name) do
if String.valid?(name) do
Nif.nif_interpolate_new(name)
|> wrap_type()
else
{:error, "expected UTF-8 binary string"}
end
end
@doc """
Make a new interpolator by name.
Make a new interpolator from the libvips class nickname. For example:
```elixir
interpolate = Interpolate.new!("bilindear")
```
You can get a list of all supported interpolators from the command-line with:
```shell
$ vips -l interpolate
```
See for example `affine`.
"""
@spec new!(String.t()) :: __MODULE__.t()
def new!(name) do
case new(name) do
{:ok, interpolate} ->
interpolate
{:error, error} ->
raise error
end
end
defp wrap_type({:ok, ref}), do: {:ok, %Interpolate{ref: ref}}
defp wrap_type(value), do: value
end