Skip to content

Commit

Permalink
Add Image.open/2 support for HEIC binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 23, 2024
1 parent e595799 commit 3face19
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

## Image 0.54.0

This is the changelog for Image version 0.54.0 released on ____, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-image/image/tags)
This is the changelog for Image version 0.54.0 released on July 24th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-image/image/tags)

### Bug Fixes

* Fix typespecs in `Image.Draw`, improve tests and clarify docs. In particular, document that the function passed to `Image.mutate/2` *must* return either `:ok` or `{:ok, term}`.

* Fix `Image.get_pixel/3` to ensure only integer values are returned when the image band format is integer. This is required because the underlying `Vix.Vips.Operation.getpoint/3` always returns floats.

* Fix using `Image.open/2` with binary HEIC files. HEIC magic numbers are now used to identify if a binary is an HEIC image (as is already done for other image types).

### Enhancements

* Adds `Image.vibrance/3` and `Image.vibrance!/3` following the [libvips discussion](https://github.com/libvips/libvips/discussions/4039).
Expand Down
15 changes: 15 additions & 0 deletions lib/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,21 @@ defmodule Image do
from_binary(image, options)
end

# 'heic': the usual HEIF images
# 'heix': 10bit images, or anything that uses h265 with range extension
# 'hevc', 'hevx': brands for image sequences
# 'heim': multiview
# 'heis': scalable
# 'hevm': multiview sequence
# 'hevs': sc

@heic_types ["heic", "heix", "hevc", "hevx", "heim", "heis", "hevm", "hevs", "mif1"]

def open(<<_::bytes-4, "ftyp", type::bytes-4, _rest::binary>> = image, options)
when type in @heic_types do
from_binary(image, options)
end

# A file path
def open(image_path, options) when is_binary(image_path) do
with {:ok, options} <- Options.Open.validate_options(options) do
Expand Down
5 changes: 5 additions & 0 deletions test/image_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,9 @@ defmodule Image.Test do
joined = Image.join_bands!(bands)
assert {:ok, +0.0, _} = Image.compare(image, joined)
end

test "Opening an HEIC binary" do
heic = File.read!("./test/support/images/sample1.heic")
assert {:ok, _image} = Image.open(heic)
end
end
Binary file added test/support/images/sample1.heic
Binary file not shown.

0 comments on commit 3face19

Please sign in to comment.