Skip to content

Commit 94d2f39

Browse files
committed
Add tests
1 parent ff46f75 commit 94d2f39

8 files changed

Lines changed: 130 additions & 9 deletions

lib/image_lens_correction.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule ImageLensCorrection do
1+
defmodule Image.LensCorrection do
22
@moduledoc """
33
44

mix.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule ImageLensCorrection.MixProject do
77
version: "0.1.0",
88
elixir: "~> 1.14",
99
start_permanent: Mix.env() == :prod,
10+
elixirc_paths: elixirc_paths(Mix.env()),
1011
deps: deps()
1112
]
1213
end
@@ -23,4 +24,17 @@ defmodule ImageLensCorrection.MixProject do
2324
{:image, path: "../image"}
2425
]
2526
end
27+
28+
defp preferred_cli_env() do
29+
[]
30+
end
31+
32+
def aliases do
33+
[]
34+
end
35+
36+
defp elixirc_paths(:test), do: ["lib", "mix", "test"]
37+
defp elixirc_paths(:dev), do: ["lib", "mix", "bench"]
38+
defp elixirc_paths(:release), do: ["lib"]
39+
defp elixirc_paths(_), do: ["lib"]
2640
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Image.DistortionCorrection.Test do
2+
use ExUnit.Case, async: true
3+
import Image.TestSupport
4+
5+
test "Image.distortion_correction/5" do
6+
image_file = "gridlines_barrel.png"
7+
validate_file = "gridlines_barrel_corrected.png"
8+
9+
image_path = image_path(image_file)
10+
validate_path = validate_path(validate_file)
11+
12+
image = Image.open!(image_path)
13+
{:ok, corrected} = Image.LensCorrection.radial_distortion_correction(image, -0.007715, 0.086731, 0.0)
14+
15+
# {:ok, _image} = Image.write(corrected, validate_path)
16+
assert_images_equal(corrected, validate_path)
17+
end
18+
end

test/image_lens_correction_test.exs

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/support/image_test_helpers.ex

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
defmodule Image.TestSupport do
2+
import ExUnit.Assertions
3+
alias Vix.Vips.Image, as: Vimage
4+
5+
@images_path Path.join(__DIR__, "images")
6+
@validate_path Path.join(__DIR__, "validate")
7+
@acceptable_similarity 1.1
8+
9+
@dialyzer {:nowarn_function, {:assert_files_equal, 2}}
10+
def assert_files_equal(expected, result) do
11+
assert File.read!(expected) == File.read!(result)
12+
end
13+
14+
@dialyzer {:nowarn_function, {:assert_images_equal, 2}}
15+
@dialyzer {:nowarn_function, {:assert_images_equal, 3}}
16+
17+
def assert_images_equal(calculated_image, validate, similarity \\ @acceptable_similarity)
18+
19+
def assert_images_equal(%Vimage{} = calculated_image, validate, similarity)
20+
when is_binary(validate) do
21+
validate_image = Image.open!(validate, access: :random)
22+
compare_images(calculated_image, validate_image, similarity)
23+
end
24+
25+
def assert_images_equal(calculated, validate, similarity)
26+
when is_binary(calculated) and is_binary(validate) do
27+
validate_image = Image.open!(validate, access: :random)
28+
calculated_image = Image.open!(calculated, access: :random)
29+
30+
compare_images(calculated_image, validate_image, similarity)
31+
end
32+
33+
def assert_images_equal(%Vimage{} = calculated, %Vimage{} = validate, similarity) do
34+
compare_images(calculated, validate, similarity)
35+
end
36+
37+
def image_path(name) do
38+
Path.join(@images_path, name)
39+
end
40+
41+
def validate_path(name) do
42+
Path.join(@validate_path, name)
43+
end
44+
45+
# From: https://github.com/libvips/libvips/discussions/2232
46+
# Calculate a single number for the match between two images, calculate the sum
47+
# of squares of differences,
48+
@dialyzer {:nowarn_function, {:compare_images, 3}}
49+
def compare_images(calculated_image, validate_image, acceptable_similarity) do
50+
alias Image.Math
51+
validate_path = Image.filename(validate_image)
52+
53+
{calculated_image, validate_image} =
54+
if Vimage.format(calculated_image) == Vimage.format(validate_image) do
55+
{calculated_image, validate_image}
56+
else
57+
{
58+
Vix.Vips.Operation.cast!(calculated_image, :VIPS_FORMAT_UCHAR),
59+
Vix.Vips.Operation.cast!(validate_image, :VIPS_FORMAT_UCHAR)
60+
}
61+
end
62+
63+
similarity =
64+
calculated_image
65+
|> Math.subtract!(validate_image)
66+
|> Math.pow!(2)
67+
|> Vix.Vips.Operation.avg!()
68+
69+
if similarity < acceptable_similarity do
70+
assert true
71+
else
72+
path = String.replace(validate_path, "validate", "did_not_match")
73+
74+
comparison_image =
75+
Vix.Vips.Operation.relational!(
76+
calculated_image,
77+
validate_image,
78+
:VIPS_OPERATION_RELATIONAL_EQUAL
79+
)
80+
81+
Image.write!(comparison_image, path)
82+
83+
flunk(
84+
"Calculated image did not match pre-existing validation image. " <>
85+
"Similarity score was #{inspect(similarity)}. " <>
86+
"See the image at #{path} for the image diff."
87+
)
88+
end
89+
end
90+
end
29.7 KB
Loading
41.5 KB
Loading

test/test_helper.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
paths = Path.wildcard("test/support/did_not_match/**/*.{png,tif,jpg,webp,gif}")
2+
Enum.each(paths, &File.rm/1)
3+
4+
Application.ensure_all_started(:telemetry)
5+
Application.ensure_all_started(:hackney)
6+
7+
ExUnit.configure(exclude: [full: true], timeout: 120_000)
18
ExUnit.start()

0 commit comments

Comments
 (0)