Skip to content

Commit

Permalink
Add formatter and appease the lint gods (#9)
Browse files Browse the repository at this point in the history
* Add formatter to lint task

* Apply formatting with mix format

* Bump version to 0.2.5
  • Loading branch information
warmwaffles authored Jun 9, 2024
1 parent ba2210a commit 8588595
Show file tree
Hide file tree
Showing 37 changed files with 640 additions and 330 deletions.
8 changes: 8 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Used by "mix format"
[
inputs: [
"{mix,.formatter}.exs",
"{lib,test,bench}/**/*.{ex,exs}"
],
line_length: 88
]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.4
0.2.5
31 changes: 25 additions & 6 deletions lib/ex_image_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,18 @@ defmodule ExImageInfo do
# but still keeping :png as the first
@types [:png, :jpeg, :gif, :bmp, :ico, :tiff, :webp, :psd, :jp2, :pnm]

@type image_format :: :png | :jpeg | :gif | :bmp | :ico | :tiff | :webp | :psd | :jp2 | :pnm
@typedoc "The supported image formats"
@type image_format ::
:png
| :jpeg
| :gif
| :bmp
| :ico
| :tiff
| :webp
| :psd
| :jp2
| :pnm

## Public API

Expand Down Expand Up @@ -176,7 +187,8 @@ defmodule ExImageInfo do
maybe_png_binary |> ExImageInfo.type :png
# nil
"""
@spec type(binary, format :: atom) :: {mimetype :: String.t, variant :: String.t} | nil
@spec type(binary, format :: atom) ::
{mimetype :: String.t(), variant :: String.t()} | nil
def type(binary, format)
def type(binary, :png), do: PNG.type(binary)
def type(binary, :gif), do: GIF.type(binary)
Expand Down Expand Up @@ -213,7 +225,7 @@ defmodule ExImageInfo do
webp_full_binary |> ExImageInfo.type
# {"image/webp", "webpVP8"}
"""
@spec type(binary) :: {mimetype :: String.t, variant :: String.t} | nil
@spec type(binary) :: {mimetype :: String.t(), variant :: String.t()} | nil
def type(binary), do: try_type(binary, @types)

@doc """
Expand Down Expand Up @@ -245,7 +257,9 @@ defmodule ExImageInfo do
# nil
"""
@spec info(binary, format :: atom) ::
{mimetype :: String.t, width :: Integer.t, height :: Integer.t, variant :: String.t} | nil
{mimetype :: String.t(), width :: Integer.t(), height :: Integer.t(),
variant :: String.t()}
| nil
def info(binary, format)
def info(binary, :png), do: PNG.info(binary)
def info(binary, :gif), do: GIF.info(binary)
Expand Down Expand Up @@ -282,19 +296,24 @@ defmodule ExImageInfo do
webp_full_binary |> ExImageInfo.info
# {"image/webp", 20, 100, "webpVP8"}
"""
@spec info(binary) :: {mimetype :: String.t, width :: Integer.t, height :: Integer.t, variant :: String.t} | nil
@spec info(binary) ::
{mimetype :: String.t(), width :: Integer.t(), height :: Integer.t(),
variant :: String.t()}
| nil
def info(binary), do: try_info(binary, @types)

## Private

@doc false
defp try_seems?(_binary, []), do: nil

defp try_seems?(binary, [type | types]) do
if seems?(binary, type), do: type, else: try_seems?(binary, types)
end

@doc false
defp try_type(_binary, []), do: nil

defp try_type(binary, [type | types]) do
case type(binary, type) do
type_t when is_tuple(type_t) -> type_t
Expand All @@ -304,11 +323,11 @@ defmodule ExImageInfo do

@doc false
defp try_info(_binary, []), do: nil

defp try_info(binary, [type | types]) do
case info(binary, type) do
info_t when is_tuple(info_t) -> info_t
_ -> try_info(binary, types)
end
end

end
7 changes: 5 additions & 2 deletions lib/ex_image_info/detector.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
defmodule ExImageInfo.Detector do
@moduledoc false

@callback info(binary) :: {mimetype :: String.t, width :: Integer.t, height :: Integer.t, variant :: String.t} | nil
@callback type(binary) :: {mimetype :: String.t, variant :: String.t} | nil
@callback info(binary) ::
{mimetype :: String.t(), width :: Integer.t(), height :: Integer.t(),
variant :: String.t()}
| nil
@callback type(binary) :: {mimetype :: String.t(), variant :: String.t()} | nil
@callback seems?(binary) :: boolean()
end
13 changes: 8 additions & 5 deletions lib/ex_image_info/types/bmp.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ExImageInfo.Types.BMP do

@moduledoc false

@behaviour ExImageInfo.Detector
Expand All @@ -11,13 +10,17 @@ defmodule ExImageInfo.Types.BMP do

## Public API

def seems?(<< @signature, _rest::binary >>), do: true
def seems?(<<@signature, _rest::binary>>), do: true
def seems?(_), do: false

def info(<< @signature, _::bytes-size(16), width::little-size(16), _::bytes-size(2), height::little-size(16), _rest::binary >>), do: {@mime, width, height, @ftype}
def info(
<<@signature, _::bytes-size(16), width::little-size(16), _::bytes-size(2),
height::little-size(16), _rest::binary>>
),
do: {@mime, width, height, @ftype}

def info(_), do: nil

def type(<< @signature, _rest::binary >>), do: {@mime, @ftype}
def type(<<@signature, _rest::binary>>), do: {@mime, @ftype}
def type(_), do: nil

end
24 changes: 16 additions & 8 deletions lib/ex_image_info/types/gif.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ExImageInfo.Types.GIF do

@moduledoc false

@behaviour ExImageInfo.Detector
Expand All @@ -11,16 +10,25 @@ defmodule ExImageInfo.Types.GIF do
@signature_87a <<"GIF87a">>
@signature_89a <<"GIF89a">>

def seems?(<< @signature_87a, _rest::binary >>), do: true
def seems?(<< @signature_89a, _rest::binary >>), do: true
def seems?(<<@signature_87a, _rest::binary>>), do: true
def seems?(<<@signature_89a, _rest::binary>>), do: true
def seems?(_), do: false

def info(<< @signature_87a, width::little-size(16), height::little-size(16), _rest::binary >>), do: {@mime, width, height, @ftype87}
def info(<< @signature_89a, width::little-size(16), height::little-size(16), _rest::binary >>), do: {@mime, width, height, @ftype89}
def info(
<<@signature_87a, width::little-size(16), height::little-size(16),
_rest::binary>>
),
do: {@mime, width, height, @ftype87}

def info(
<<@signature_89a, width::little-size(16), height::little-size(16),
_rest::binary>>
),
do: {@mime, width, height, @ftype89}

def info(_), do: nil

def type(<< @signature_89a, _rest::binary >>), do: {@mime, @ftype89}
def type(<< @signature_87a, _rest::binary >>), do: {@mime, @ftype87}
def type(<<@signature_89a, _rest::binary>>), do: {@mime, @ftype89}
def type(<<@signature_87a, _rest::binary>>), do: {@mime, @ftype87}
def type(_), do: nil

end
23 changes: 15 additions & 8 deletions lib/ex_image_info/types/ico.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ExImageInfo.Types.ICO do

@moduledoc false

@behaviour ExImageInfo.Detector
Expand All @@ -8,39 +7,47 @@ defmodule ExImageInfo.Types.ICO do
@mime "image/x-icon"
@ftype "ICO"

@signature << 0::size(16), 0x01, 0x00 >> # 0x0100 -> little endian -> 1 (.ICO), 2 (.CUR)
# 0x0100 -> little endian -> 1 (.ICO), 2 (.CUR)
@signature <<0::size(16), 0x01, 0x00>>

## Public API

def seems?(<< @signature, _rest::binary >>), do: true
def seems?(<<@signature, _rest::binary>>), do: true
def seems?(_), do: false

def info(<< @signature, num_images::little-size(16), rest::binary >>) do
def info(<<@signature, num_images::little-size(16), rest::binary>>) do
case parse(rest, num_images) do
{w, h} -> {@mime, w, h, @ftype}
_ -> nil
end
end

def info(_), do: nil

def type(<< @signature, _rest::binary >>), do: {@mime, @ftype}
def type(<<@signature, _rest::binary>>), do: {@mime, @ftype}
def type(_), do: nil

## Private

defp parse(binary, num), do: parse(binary, num, {0, 0})
defp parse(<< w::size(8), h::size(8), _head::bytes-size(14), rest::binary >>, num, {wp, hp} = acc) do

defp parse(
<<w::size(8), h::size(8), _head::bytes-size(14), rest::binary>>,
num,
{wp, hp} = acc
) do
w = if w == 0, do: 256, else: w
h = if h == 0, do: 256, else: h
sump = wp + hp
sum = w + h
acc = if sum > sump, do: {w, h}, else: acc
if num == 1 do # last one
# last one
if num == 1 do
acc
else
parse(rest, num - 1, acc)
end
end
defp parse(_, _, _), do: nil

defp parse(_, _, _), do: nil
end
17 changes: 10 additions & 7 deletions lib/ex_image_info/types/jp2.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ExImageInfo.Types.JP2 do

@moduledoc false

@behaviour ExImageInfo.Detector
Expand All @@ -9,18 +8,22 @@ defmodule ExImageInfo.Types.JP2 do
@mime "image/jp2"
@ftype "JP2"

@signature << 0::size(24), 0x0c6A5020200D0A870A::size(72) >>
@signature_ihdr << "ihdr" >>
@signature <<0::size(24), 0x0C6A5020200D0A870A::size(72)>>
@signature_ihdr <<"ihdr">>

## Public API

def seems?(<< @signature, _rest::binary >>), do: true
def seems?(<<@signature, _rest::binary>>), do: true
def seems?(_), do: false

def info(<< @signature, _::bytes-size(32), @signature_ihdr, h::size(32), w::size(32), _rest::binary >>), do: {@mime, w, h, @ftype}
def info(
<<@signature, _::bytes-size(32), @signature_ihdr, h::size(32), w::size(32),
_rest::binary>>
),
do: {@mime, w, h, @ftype}

def info(_), do: nil

def type(<< @signature, _rest::binary >>), do: {@mime, @ftype}
def type(<<@signature, _rest::binary>>), do: {@mime, @ftype}
def type(_), do: nil

end
31 changes: 20 additions & 11 deletions lib/ex_image_info/types/jpeg.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ExImageInfo.Types.JPEG do

@moduledoc false

@behaviour ExImageInfo.Detector
Expand All @@ -9,14 +8,14 @@ defmodule ExImageInfo.Types.JPEG do
@ftype_base "baseJPEG"
@ftype_prog "progJPEG"

@signature << 0xFFD8::size(16) >>
@signature <<0xFFD8::size(16)>>

## Public API

def seems?(<< @signature, _rest::binary >>), do: true
def seems?(<<@signature, _rest::binary>>), do: true
def seems?(_), do: false

def info(<< @signature, _::size(16), rest::binary >>), do: parse_jpeg(rest)
def info(<<@signature, _::size(16), rest::binary>>), do: parse_jpeg(rest)
def info(_), do: nil

def type(bin) do
Expand All @@ -28,27 +27,37 @@ defmodule ExImageInfo.Types.JPEG do

## Private

defp parse_jpeg(<< block_len::size(16), rest::binary >>) do
defp parse_jpeg(<<block_len::size(16), rest::binary>>) do
parse_jpeg_block(block_len, rest)
end

defp parse_jpeg(_), do: nil

defp parse_jpeg_block(block_len, << rest::binary >>) do
block_len = block_len - 2 # bytes of block_len
defp parse_jpeg_block(block_len, <<rest::binary>>) do
# bytes of block_len
block_len = block_len - 2

case rest do
<< _::bytes-size(block_len), 0xFF, sof::size(8), next::binary >> -> parse_jpeg_sof(sof, next)
_ -> nil
<<_::bytes-size(block_len), 0xFF, sof::size(8), next::binary>> ->
parse_jpeg_sof(sof, next)

_ ->
nil
end
end

defp parse_jpeg_block(_, _), do: nil

defp parse_jpeg_sof(0xC0, next), do: parse_jpeg_dimensions(@ftype_base, next)
defp parse_jpeg_sof(0xC2, next), do: parse_jpeg_dimensions(@ftype_prog, next)
defp parse_jpeg_sof(_, next), do: parse_jpeg(next)

defp parse_jpeg_dimensions(ftype, << _skip::size(24), height::size(16), width::size(16), _::binary >>) do
defp parse_jpeg_dimensions(
ftype,
<<_skip::size(24), height::size(16), width::size(16), _::binary>>
) do
{@mime, width, height, ftype}
end
defp parse_jpeg_dimensions(_, _), do: nil

defp parse_jpeg_dimensions(_, _), do: nil
end
15 changes: 10 additions & 5 deletions lib/ex_image_info/types/png.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ExImageInfo.Types.PNG do

@moduledoc false

@behaviour ExImageInfo.Detector
Expand All @@ -12,13 +11,19 @@ defmodule ExImageInfo.Types.PNG do

## Public API

def seems?(<< _::bytes-size(1), @signature, _rest::binary >>), do: true
def seems?(<<_::bytes-size(1), @signature, _rest::binary>>), do: true
def seems?(_), do: false

def info(<< _::bytes-size(1), @signature, _::size(32), @signature_ihdr, width::size(32), height::size(32), _rest::binary >>), do: {@mime, width, height, @ftype}
def info(
<<_::bytes-size(1), @signature, _::size(32), @signature_ihdr, width::size(32),
height::size(32), _rest::binary>>
),
do: {@mime, width, height, @ftype}

def info(_), do: nil

def type(<< _::size(8), @signature, _::size(32), @signature_ihdr, _rest::binary >>), do: {@mime, @ftype}
def type(_), do: nil
def type(<<_::size(8), @signature, _::size(32), @signature_ihdr, _rest::binary>>),
do: {@mime, @ftype}

def type(_), do: nil
end
Loading

0 comments on commit 8588595

Please sign in to comment.