diff --git a/lib/image/options/embed.ex b/lib/image/options/embed.ex index 62f531c7..a7ac53a8 100644 --- a/lib/image/options/embed.ex +++ b/lib/image/options/embed.ex @@ -173,13 +173,13 @@ 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) @@ -187,7 +187,7 @@ defmodule Image.Options.Embed do defp adjust_transparency(%{extend_mode: :VIPS_EXTEND_BACKGROUND} = options, 1, _has_alpha?) do background_color = - options.background + options.background_color |> hd() |> List.wrap() diff --git a/test/embed_test.exs b/test/embed_test.exs index 4332c5b3..37732b49 100644 --- a/test/embed_test.exs +++ b/test/embed_test.exs @@ -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 diff --git a/test/support/validate/embed/penguin_with_alpha_embed_background.png b/test/support/validate/embed/penguin_with_alpha_embed_background.png new file mode 100644 index 00000000..067d2342 Binary files /dev/null and b/test/support/validate/embed/penguin_with_alpha_embed_background.png differ diff --git a/test/support/validate/embed/sydney_opera_house_bw_embed_background.png b/test/support/validate/embed/sydney_opera_house_bw_embed_background.png new file mode 100644 index 00000000..99ccf06d Binary files /dev/null and b/test/support/validate/embed/sydney_opera_house_bw_embed_background.png differ