Skip to content

Commit 17a9fd3

Browse files
committed
Document types in Turbojpeg
Documenting is important and types are important to the external interface of the library. To be able to document I had to add a wrapper to the Native code. I think that is okay for us to add a layer in front of the native for a little more control. I decided to capture the exceptions coming from the native code because I'm not sure if the Elixir processes using this should die if the native code has issues. Not the choice is in the hands of the user.
1 parent a174534 commit 17a9fd3

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

dialyzer.ignore-warnings

Whitespace-only changes.

lib/turbojpeg.ex

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
11
defmodule Turbojpeg do
22
@moduledoc File.read!("README.md")
3+
alias Turbojpeg.Native
4+
5+
@type width :: dimension
6+
@type height :: dimension
7+
@type dimension :: pos_integer()
8+
@type quality :: 0..100
9+
@type format ::
10+
:I420
11+
| :I422
12+
| :I444
13+
| :GRAY
14+
@type error ::
15+
{:error, atom()}
16+
| {:error, struct}
17+
@type jpeg_header :: %{
18+
format: format(),
19+
width: width(),
20+
height: height()
21+
}
22+
23+
@spec yuv_to_jpeg(Shmex.t(), width, height, quality, format) ::
24+
{:ok, Shmex.t()} | error()
25+
26+
def yuv_to_jpeg(yuv, width, height, quality, format) do
27+
Native.yuv_to_jpeg(yuv, width, height, quality, format)
28+
rescue
29+
error ->
30+
{:error, error}
31+
end
32+
33+
@spec jpeg_to_yuv(Shmex.t()) ::
34+
{:ok, Shmex.t()} | error()
35+
36+
def jpeg_to_yuv(jpeg) do
37+
Native.jpeg_to_yuv(jpeg)
38+
rescue
39+
error ->
40+
{:error, error}
41+
end
42+
43+
@spec get_jpeg_header(Shmex.t()) :: {:ok, jpeg_header} | error()
44+
def get_jpeg_header(jpeg) do
45+
Native.get_jpeg_header(jpeg)
46+
rescue
47+
error ->
48+
{:error, error}
49+
end
350
end

lib/turbojpeg/sink.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
defmodule Turbojpeg.Sink do
2-
alias Turbojpeg.Native
32
use Membrane.Sink
43
alias Membrane.{Buffer, Time}
54
alias Membrane.Caps.Video.Raw
@@ -48,12 +47,12 @@ defmodule Turbojpeg.Sink do
4847
@impl true
4948
def handle_write(:input, %Buffer{payload: payload}, _ctx, state) do
5049
with {:ok, data} <-
51-
Native.yuv_to_jpeg(payload, state.width, state.height, state.quality, state.format),
50+
Turbojpeg.yuv_to_jpeg(payload, state.width, state.height, state.quality, state.format),
5251
:ok <- File.write(state.filename, Shmex.to_binary(data)) do
5352
{:ok, state}
5453
else
55-
{:error, reason} ->
56-
{{:error, reason}, state}
54+
{:error, _} = error ->
55+
{error, state}
5756
end
5857
end
5958

mix.exs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ defmodule Turbojpeg.MixProject do
1616
homepage_url: @github_link,
1717
package: package(),
1818
docs: docs(),
19-
deps: deps()
19+
deps: deps(),
20+
dialyzer: [
21+
ignore_warnings: "dialyzer.ignore-warnings"
22+
]
2023
]
2124
end
2225

@@ -36,7 +39,8 @@ defmodule Turbojpeg.MixProject do
3639
{:membrane_core, "~> 0.5.0"},
3740
{:ex_doc, "~> 0.21.3", only: [:dev], runtime: false},
3841
{:propcheck, "~> 1.2.0", only: [:test]},
39-
{:mogrify, github: "ConnorRigby/mogrify", branch: "master", only: [:test, :dev]}
42+
{:mogrify, github: "ConnorRigby/mogrify", branch: "master", only: [:test, :dev]},
43+
{:dialyxir, "~> 1.0.0-rc.7", only: [:dev], runtime: false}
4044
]
4145
end
4246

mix.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"bunch_native": {:hex, :bunch_native, "0.2.1", "0227d2a751a32f8c0b77dfec57c8dc7216351720c9c755c467e6d9387467fd1f", [:mix], [{:bundlex, "~> 0.2.7", [hex: :bundlex, repo: "hexpm", optional: false]}], "hexpm", "f0819b2f9f78086447ac7459a8e7b6e25ad535b9d3a4f9469253c552137de6b4"},
44
"bundlex": {:hex, :bundlex, "0.2.7", "8f46199bf4cf84a60cdfc142edeafbab37040167acc35dda8aa70433f2ff8162", [:mix], [{:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.5", [hex: :qex, repo: "hexpm", optional: false]}, {:secure_random, "~> 0.5", [hex: :secure_random, repo: "hexpm", optional: false]}], "hexpm", "5751b4bb5dd0576ca70519239e2ee7b495b17e4bbfbe3ef1f60ae0c12c0e4fe9"},
55
"coerce": {:hex, :coerce, "1.0.1", "211c27386315dc2894ac11bc1f413a0e38505d808153367bd5c6e75a4003d096", [:mix], [], "hexpm", "b44a691700f7a1a15b4b7e2ff1fa30bebd669929ac8aa43cffe9e2f8bf051cf1"},
6+
"dialyxir": {:hex, :dialyxir, "1.0.0-rc.7", "6287f8f2cb45df8584317a4be1075b8c9b8a69de8eeb82b4d9e6c761cf2664cd", [:mix], [{:erlex, ">= 0.2.5", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "506294d6c543e4e5282d4852aead19ace8a35bedeb043f9256a06a6336827122"},
67
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
8+
"erlex": {:hex, :erlex, "0.2.5", "e51132f2f472e13d606d808f0574508eeea2030d487fc002b46ad97e738b0510", [:mix], [], "hexpm", "756d3e19b056339af674b715fdd752c5dac468cf9d0e2d1a03abf4574e99fbf8"},
79
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
810
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
911
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},

test/turbojpeg_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ defmodule TurbojpegTest do
4949
Shmex.new(jpeg.buffer)
5050
end)
5151

52-
{native_micros, ret} =
52+
{_native_micros, ret} =
5353
:timer.tc(fn ->
5454
Turbojpeg.Native.jpeg_to_yuv(jpeg)
5555
end)

0 commit comments

Comments
 (0)