-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for the AVIF, HEIC and HEIF formats
These 3 formats have in common that they use the HEIF container format specified in ISO/IEC 23008-12. The MIME types and variant types (file extension) for HEIC and HEIF are based on sections C.2 and D.2 of the specification. A HEIF container can contain multiple images, so the logic is kept as in `:ico` (take the size of the largest image). The size of an image is found in the `ispe` box.
- Loading branch information
Showing
5 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule ExImageInfo.Types.AVIF do | ||
@moduledoc false | ||
|
||
@behaviour ExImageInfo.Detector | ||
|
||
@mime "image/avif" | ||
@signature <<"ftypavif">> | ||
@ftype "AVIF" | ||
|
||
## Public API | ||
|
||
def seems?(<<_::size(32), @signature, _rest::binary>>), do: true | ||
def seems?(_), do: false | ||
|
||
def info(<<_::size(32), @signature, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime, @ftype) | ||
|
||
def info(_), do: nil | ||
|
||
def type(<<_::size(32), @signature, _rest::binary>>), do: {@mime, @ftype} | ||
def type(_), do: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule ExImageInfo.Types.HEIC do | ||
@moduledoc false | ||
|
||
@behaviour ExImageInfo.Detector | ||
|
||
@mime_heic "image/heic" | ||
@mime_heic_sequence "image/heic-sequence" | ||
@signature_heic <<"ftypheic">> | ||
@signature_heim <<"ftypheim">> | ||
@signature_heis <<"ftypheis">> | ||
@signature_heix <<"ftypheix">> | ||
@signature_hevc <<"ftyphevc">> | ||
@signature_hevm <<"ftyphevm">> | ||
@signature_hevs <<"ftyphevs">> | ||
@signature_hevx <<"ftyphevx">> | ||
@ftype_heic "HEIC" | ||
@ftype_heic_sequence "HEICS" | ||
|
||
## Public API | ||
|
||
def seems?(<<_::size(32), @signature_heic, _rest::binary>>), do: true | ||
def seems?(<<_::size(32), @signature_heim, _rest::binary>>), do: true | ||
def seems?(<<_::size(32), @signature_heis, _rest::binary>>), do: true | ||
def seems?(<<_::size(32), @signature_heix, _rest::binary>>), do: true | ||
def seems?(_), do: false | ||
|
||
def info(<<_::size(32), @signature_heic, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic, @ftype_heic) | ||
|
||
def info(<<_::size(32), @signature_heim, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic, @ftype_heic) | ||
|
||
def info(<<_::size(32), @signature_heis, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic, @ftype_heic) | ||
|
||
def info(<<_::size(32), @signature_heix, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic, @ftype_heic) | ||
|
||
def info(<<_::size(32), @signature_hevc, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic_sequence, @ftype_heic_sequence) | ||
|
||
def info(<<_::size(32), @signature_hevm, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic_sequence, @ftype_heic_sequence) | ||
|
||
def info(<<_::size(32), @signature_hevs, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic_sequence, @ftype_heic_sequence) | ||
|
||
def info(<<_::size(32), @signature_hevx, rest::binary>>), | ||
do: ExImageInfo.Types.HEIF.info(rest, @mime_heic_sequence, @ftype_heic_sequence) | ||
|
||
def info(_), do: nil | ||
|
||
def type(<<_::size(32), @signature_heic, _rest::binary>>), | ||
do: {@mime_heic, @ftype_heic} | ||
|
||
def type(<<_::size(32), @signature_heim, _rest::binary>>), | ||
do: {@mime_heic, @ftype_heic} | ||
|
||
def type(<<_::size(32), @signature_heis, _rest::binary>>), | ||
do: {@mime_heic, @ftype_heic} | ||
|
||
def type(<<_::size(32), @signature_heix, _rest::binary>>), | ||
do: {@mime_heic, @ftype_heic} | ||
|
||
def type(<<_::size(32), @signature_hevc, _rest::binary>>), | ||
do: {@mime_heic_sequence, @ftype_heic_sequence} | ||
|
||
def type(<<_::size(32), @signature_hevm, _rest::binary>>), | ||
do: {@mime_heic_sequence, @ftype_heic_sequence} | ||
|
||
def type(<<_::size(32), @signature_hevs, _rest::binary>>), | ||
do: {@mime_heic_sequence, @ftype_heic_sequence} | ||
|
||
def type(<<_::size(32), @signature_hevx, _rest::binary>>), | ||
do: {@mime_heic_sequence, @ftype_heic_sequence} | ||
|
||
def type(_), do: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule ExImageInfo.Types.HEIF do | ||
@moduledoc false | ||
|
||
@behaviour ExImageInfo.Detector | ||
|
||
@mime_heif "image/heif" | ||
@mime_heif_sequence "image/heif-sequence" | ||
@signature_mif1 <<"ftypmif1">> | ||
@signature_msf1 <<"ftypmsf1">> | ||
@ftype_heif "HEIF" | ||
@ftype_heif_sequence "HEIFS" | ||
|
||
## Public API | ||
|
||
def seems?(<<_::size(32), @signature_mif1, _rest::binary>>), do: true | ||
def seems?(<<_::size(32), @signature_msf1, _rest::binary>>), do: true | ||
def seems?(_), do: false | ||
|
||
def info(<<_::size(32), @signature_mif1, rest::binary>>) do | ||
info(rest, @mime_heif, @ftype_heif) | ||
end | ||
|
||
def info(<<_::size(32), @signature_msf1, rest::binary>>), | ||
do: info(rest, @mime_heif_sequence, @ftype_heif_sequence) | ||
|
||
def info(_), do: nil | ||
|
||
def info(binary, mime, ftype) do | ||
case size(binary) do | ||
{w, h} -> {mime, w, h, ftype} | ||
_ -> nil | ||
end | ||
end | ||
|
||
def type(<<_::size(32), @signature_mif1, _rest::binary>>), | ||
do: {@mime_heif, @ftype_heif} | ||
|
||
def type(<<_::size(32), @signature_msf1, _rest::binary>>), | ||
do: {@mime_heif_sequence, @ftype_heif_sequence} | ||
|
||
def type(_), do: nil | ||
|
||
defp size(binary) do | ||
[_hd | ispe_boxes] = String.split(binary, "ispe") | ||
|
||
if length(ispe_boxes) > 0 do | ||
{width, height} = | ||
ispe_boxes | ||
|> Enum.map(fn <<_::size(32), width::size(32), height::size(32), _rest::binary>> -> | ||
{width, height} | ||
end) | ||
|> Enum.max_by(&elem(&1, 0)) | ||
|
||
{width, height} | ||
else | ||
nil | ||
end | ||
end | ||
end |