Skip to content

Commit 82f813a

Browse files
committed
Fix Image.rounded/1 and Image.squircle/1 when the image has an alpha band. Closes #182
1 parent 62fd37e commit 82f813a

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## Image 0.59.2
4+
5+
This is the changelog for Image version 0.59.1 released on April 21st, 2025. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-image/image/tags)
6+
7+
### Bug Fixes
8+
9+
* Fixes `Image.rounded/1` and `Image.squircle/1` when the image has an alpha band. Thanks to @Neophen for the report. Closes #182.
10+
311
## Image 0.59.1
412

513
This is the changelog for Image version 0.59.1 released on April 21st, 2025. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-image/image/tags)

lib/image.ex

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6049,16 +6049,26 @@ defmodule Image do
60496049
* `{:error, reason}`
60506050
60516051
"""
6052-
@doc subject: "Generator"
6052+
@doc subject: "Mask"
60536053

60546054
@spec rounded(Vimage.t(), Keyword.t()) :: {:ok, Vimage.t()} | {:error, error_message()}
60556055
def rounded(%Vimage{} = image, options \\ []) do
6056+
use Image.Math
6057+
60566058
options = Keyword.put_new(options, :radius, @default_round_corner_radius)
60576059
width = width(image)
60586060
height = height(image)
60596061

60606062
{:ok, mask} = mask(:rounded_corners, width, height, options)
6061-
Operation.bandjoin([image, mask])
6063+
6064+
case split_alpha(image) do
6065+
{base_image, nil} ->
6066+
Operation.bandjoin([base_image, mask])
6067+
6068+
{base_image, alpha} ->
6069+
alpha = if_then_else!(mask == 0, mask, alpha)
6070+
Operation.bandjoin([base_image, alpha])
6071+
end
60626072
end
60636073

60646074
@doc """
@@ -6118,12 +6128,22 @@ defmodule Image do
61186128

61196129
@spec squircle(Vimage.t(), Keyword.t()) :: {:ok, Vimage.t()} | {:error, error_message()}
61206130
def squircle(%Vimage{} = image, options \\ []) do
6131+
use Image.Math
6132+
61216133
options = Keyword.put_new(options, :radius, @default_squircle_radius)
61226134
width = width(image)
61236135
height = height(image)
61246136

61256137
{:ok, mask} = mask(:squircle, width, height, options)
6126-
Operation.bandjoin([image, mask])
6138+
6139+
case split_alpha(image) do
6140+
{base_image, nil} ->
6141+
Operation.bandjoin([base_image, mask])
6142+
6143+
{base_image, alpha} ->
6144+
alpha = if_then_else!(mask == 0, mask, alpha)
6145+
Operation.bandjoin([base_image, alpha])
6146+
end
61276147
end
61286148

61296149
@doc """

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Image.MixProject do
22
use Mix.Project
33

4-
@version "0.59.1"
4+
@version "0.59.2"
55

66
@app_name "image"
77

test/image_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,38 @@ defmodule Image.Test do
6767
assert_images_equal(out_path, validate_path("Kip_small_squircle_mask.png"))
6868
end
6969

70+
test "Rounded Image with alpha", %{dir: dir} do
71+
image = image_path("Kip_small.jpg")
72+
validate_path = validate_path("Kip_small_alpha_rounded_mask.png")
73+
74+
{:ok, kip} = Vimage.new_from_file(image)
75+
{:ok, kip} = Image.add_alpha(kip, 255)
76+
{:ok, rounded} = Image.rounded(kip)
77+
78+
out_path = Temp.path!(suffix: ".png", basedir: dir)
79+
assert :ok = Vimage.write_to_file(rounded, out_path)
80+
81+
# {:ok, _} = Image.write(rounded, validate_path)
82+
83+
assert_images_equal(out_path, validate_path)
84+
end
85+
86+
test "Squircled Image with alpha", %{dir: dir} do
87+
image = image_path("Kip_small.jpg")
88+
validate_path = validate_path("Kip_small_alpha_squircle_mask.png")
89+
90+
{:ok, kip} = Vimage.new_from_file(image)
91+
{:ok, kip} = Image.add_alpha(kip, 255)
92+
{:ok, squircled} = Image.squircle(kip)
93+
94+
out_path = Temp.path!(suffix: ".png", basedir: dir)
95+
assert :ok = Vimage.write_to_file(squircled, out_path)
96+
97+
# {:ok, _} = Image.write(squircled, validate_path)
98+
99+
assert_images_equal(out_path, validate_path)
100+
end
101+
70102
test "Image composition", %{dir: dir} do
71103
image = image_path("lamborghini-forsennato-concept.jpg")
72104

71.2 KB
Loading
72 KB
Loading

0 commit comments

Comments
 (0)