Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for the AVIF, HEIC and HEIF formats #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Supported formats (image type to be parsed as):
- `:psd`
- `:tiff`
- `:webp` (VP8X animated in `v0.2.4`)
- `:avif`
- `:heic`
- `:heif`

## Mime-types and Variants

Expand All @@ -68,9 +71,14 @@ Each mime-type can be linked to at least one variant type:

| mime-type | variant type | description |
| ------------------------- | ------------ | ------------------ |
| `image/avif` | `AVIF` | |
| `image/bmp` | `BMP` | |
| `image/gif` | `GIF87a` | 87a gif spec |
| `image/gif` | `GIF89a` | 89a gif spec |
| `image/heic` | `HEIC` | |
| `image/heic-sequence` | `HEICS` | |
| `image/heif` | `HEIF` | |
| `image/heif-sequence` | `HEIFS` | |
| `image/x-icon` | `ICO` | |
| `image/jpeg` | `baseJPEG` | baseline JPEG |
| `image/jpeg` | `progJPEG` | progressive JPEG |
Expand All @@ -89,10 +97,10 @@ Each mime-type can be linked to at least one variant type:
The variant type is created just to provide a bit more of information
for every image format (if applicable).

*Note*: `:ico` returns the dimensions of the largest image contained (not the first found).
*Note*: `:avif`, `:heic`, `:heif` and `:ico` return the dimensions of the largest image contained (not the first found).

The guessing functions try to detect the format of the binary by testing every available type based on its global usage (popularity, [usage of image file formats](https://w3techs.com/technologies/overview/image_format/all), but still keeping the `:png` as the first one):
- `:png`, `:jpeg`, `:gif`, `:bmp`, `:ico`, `:tiff`, `:webp`, `:psd`, `:jp2`, `:pnm`
- `:png`, `:jpeg`, `:gif`, `:bmp`, `:ico`, `:tiff`, `:webp`, `:psd`, `:jp2`, `:pnm`, `:avif`, `:heic`, `:heif`

**Warnings:**

Expand Down
38 changes: 36 additions & 2 deletions lib/ex_image_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ defmodule ExImageInfo do

| mime-type | variant type | description |
| ------------------------- | ------------ | ------------------ |
| `image/avif` | `AVIF` | |
| `image/bmp` | `BMP` | |
| `image/gif` | `GIF87a` | 87a gif spec |
| `image/gif` | `GIF89a` | 89a gif spec |
| `image/heic` | `HEIC` | |
| `image/heic-sequence` | `HEICS` | |
| `image/heif` | `HEIF` | |
| `image/heif-sequence` | `HEIFS` | |
| `image/x-icon` | `ICO` | |
| `image/jpeg` | `baseJPEG` | baseline JPEG |
| `image/jpeg` | `progJPEG` | progressive JPEG |
Expand All @@ -65,10 +70,13 @@ defmodule ExImageInfo do
*Note*: `:ico` returns the dimensions of the largest image contained (not the first found).

The guessing functions try to detect the format of the binary by testing every available type based on its global usage (popularity, [usage of image file formats](https://w3techs.com/technologies/overview/image_format/all), but still keeping the `:png` as the first one):
- `:png`, `:jpeg`, `:gif`, `:bmp`, `:ico`, `:tiff`, `:webp`, `:psd`, `:jp2`, `:pnm`
- `:png`, `:jpeg`, `:gif`, `:bmp`, `:ico`, `:tiff`, `:webp`, `:psd`, `:jp2`, `:pnm`, `:avif`, `:heic`, `:heif`
"""
alias ExImageInfo.Types.AVIF
alias ExImageInfo.Types.BMP
alias ExImageInfo.Types.GIF
alias ExImageInfo.Types.HEIC
alias ExImageInfo.Types.HEIF
alias ExImageInfo.Types.ICO
alias ExImageInfo.Types.JP2
alias ExImageInfo.Types.JPEG
Expand All @@ -81,7 +89,21 @@ defmodule ExImageInfo do
# Guessing function ordered by global usage
# https://w3techs.com/technologies/overview/image_format/all
# but still keeping :png as the first
@types [:png, :jpeg, :gif, :bmp, :ico, :tiff, :webp, :psd, :jp2, :pnm]
@types [
:png,
:jpeg,
:gif,
:bmp,
:ico,
:tiff,
:webp,
:psd,
:jp2,
:pnm,
:avif,
:heic,
:heif
]

@typedoc "The supported image formats"
@type image_format ::
Expand All @@ -95,6 +117,9 @@ defmodule ExImageInfo do
| :psd
| :jp2
| :pnm
| :avif
| :heic
| :heif

## Public API

Expand Down Expand Up @@ -138,6 +163,9 @@ defmodule ExImageInfo do
def seems?(binary, :jp2), do: JP2.seems?(binary)
def seems?(binary, :pnm), do: PNM.seems?(binary)
def seems?(binary, :ico), do: ICO.seems?(binary)
def seems?(binary, :avif), do: AVIF.seems?(binary)
def seems?(binary, :heic), do: HEIC.seems?(binary)
def seems?(binary, :heif), do: HEIF.seems?(binary)
def seems?(_, _), do: nil

@doc """
Expand Down Expand Up @@ -209,6 +237,9 @@ defmodule ExImageInfo do
def type(binary, :jp2), do: JP2.type(binary)
def type(binary, :pnm), do: PNM.type(binary)
def type(binary, :ico), do: ICO.type(binary)
def type(binary, :avif), do: AVIF.type(binary)
def type(binary, :heic), do: HEIC.type(binary)
def type(binary, :heif), do: HEIF.type(binary)
def type(_, _), do: nil

@doc """
Expand Down Expand Up @@ -280,6 +311,9 @@ defmodule ExImageInfo do
def info(binary, :jp2), do: JP2.info(binary)
def info(binary, :pnm), do: PNM.info(binary)
def info(binary, :ico), do: ICO.info(binary)
def info(binary, :avif), do: AVIF.info(binary)
def info(binary, :heic), do: HEIC.info(binary)
def info(binary, :heif), do: HEIF.info(binary)
def info(_, _), do: nil

@doc """
Expand Down
24 changes: 24 additions & 0 deletions lib/ex_image_info/types/avif.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule ExImageInfo.Types.AVIF do
@moduledoc false

@behaviour ExImageInfo.Detector

alias ExImageInfo.Types.HEIF

@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: HEIF.info(rest, @mime, @ftype)

def info(_), do: nil

def type(<<_::size(32), @signature, _rest::binary>>), do: {@mime, @ftype}
def type(_), do: nil
end
80 changes: 80 additions & 0 deletions lib/ex_image_info/types/heic.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
defmodule ExImageInfo.Types.HEIC do
@moduledoc false

@behaviour ExImageInfo.Detector

alias ExImageInfo.Types.HEIF

@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: HEIF.info(rest, @mime_heic, @ftype_heic)

def info(<<_::size(32), @signature_heim, rest::binary>>),
do: HEIF.info(rest, @mime_heic, @ftype_heic)

def info(<<_::size(32), @signature_heis, rest::binary>>),
do: HEIF.info(rest, @mime_heic, @ftype_heic)

def info(<<_::size(32), @signature_heix, rest::binary>>),
do: HEIF.info(rest, @mime_heic, @ftype_heic)

def info(<<_::size(32), @signature_hevc, rest::binary>>),
do: HEIF.info(rest, @mime_heic_sequence, @ftype_heic_sequence)

def info(<<_::size(32), @signature_hevm, rest::binary>>),
do: HEIF.info(rest, @mime_heic_sequence, @ftype_heic_sequence)

def info(<<_::size(32), @signature_hevs, rest::binary>>),
do: HEIF.info(rest, @mime_heic_sequence, @ftype_heic_sequence)

def info(<<_::size(32), @signature_hevx, rest::binary>>),
do: 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
70 changes: 70 additions & 0 deletions lib/ex_image_info/types/heif.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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>> ->
[_ispe | clap] = String.split(rest, "clap")

if length(clap) > 0 do
<<clap_width_n::size(32), clap_width_d::size(32), clap_height_n::size(32),
clap_height_d::size(32), _horizontal_offset_n::size(32),
_horizontal_offset_d::size(32), _vertical_offset_n::size(32),
_vertical_offset_d::size(32), _rest::binary>> = List.first(clap)

clap_width = round(clap_width_n / clap_width_d)
clap_height = round(clap_height_n / clap_height_d)
{clap_width, clap_height}
else
{width, height}
end
end)
|> Enum.max_by(&elem(&1, 0))

{width, height}
end
end
end