Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/image/options/embed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,21 @@ defmodule Image.Options.Embed do
bands,
true = _has_alpha?
) do
if length(options.background) == bands do
if length(options.background_color) == bands do
options
else
options
|> Map.put(
:background_color,
List.insert_at(options.background, -1, options.background_transparency)
List.insert_at(options.background_color, -1, options.background_transparency)
)
end
|> Map.delete(:background_transparency)
end

defp adjust_transparency(%{extend_mode: :VIPS_EXTEND_BACKGROUND} = options, 1, _has_alpha?) do
background_color =
options.background
options.background_color
|> hd()
|> List.wrap()

Expand Down
36 changes: 36 additions & 0 deletions test/embed_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,40 @@ defmodule Image.Embed.Test do
# {:ok, _image} = Image.write(embedded, validate_path)
assert_images_equal(embedded, validate_path)
end

test "Image.embed/4 with extend_mode: :background on an image with an alpha band" do
image_file = "penguin_with_alpha.png"
validate_file = "embed/penguin_with_alpha_embed_background.png"

image_path = image_path(image_file)
validate_path = validate_path(validate_file)

image = Image.open!(image_path, access: :random)
assert Image.has_alpha?(image)

{width, height, _bands} = Image.shape(image)
{:ok, embedded} = Image.embed(image, width, height + 50, x: 0, y: 0, extend_mode: :background)

# {:ok, _image} = Image.write(embedded, validate_path)
assert_images_equal(embedded, validate_path)
end

test "Image.embed/4 with extend_mode: :background on a single-band image" do
validate_file = "embed/sydney_opera_house_bw_embed_background.png"
validate_path = validate_path(validate_file)

image =
image_path("Sydney-Opera-House-BW.jpg")
|> Image.open!(access: :random)
|> Image.to_colorspace!(:bw)

{width, height, bands} = Image.shape(image)
assert bands == 1
refute Image.has_alpha?(image)

{:ok, embedded} = Image.embed(image, width, height + 50, x: 0, y: 0, extend_mode: :background)

# {:ok, _image} = Image.write(embedded, validate_path)
assert_images_equal(embedded, validate_path)
end
end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.