Skip to content

Commit 053bf5d

Browse files
authored
Adding Styler + PNM format improvement (#10)
* feat: applying styler * refact+fix+test(pnm): do not parse size if signature is not fully formed (pnm) + applying styler to pnm * chore: bump VERSION * lint(pnm): enabling nesting exception for credo * chore: bumping styler version (stable)
1 parent 8588595 commit 053bf5d

29 files changed

+85
-31
lines changed

.formatter.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Used by "mix format"
22
[
3+
plugins: [Styler],
34
inputs: [
45
"{mix,.formatter}.exs",
56
"{lib,test,bench}/**/*.{ex,exs}"

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.5
1+
0.2.6

lib/ex_image_info.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
defmodule ExImageInfo do
2-
alias ExImageInfo.Types.{BMP, GIF, ICO, JP2, JPEG, PNG, PNM, PSD, TIFF, WEBP}
3-
42
@moduledoc """
53
ExImageInfo is an Elixir library to parse images (binaries) and get the dimensions (size), detected mime-type and overall validity for a set of image formats. Main module to parse a binary and get if it seems to be an image (validity), the mime-type (and variant detected) and the dimensions of the image, based on a specific image format.
64
@@ -69,6 +67,16 @@ defmodule ExImageInfo do
6967
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):
7068
- `:png`, `:jpeg`, `:gif`, `:bmp`, `:ico`, `:tiff`, `:webp`, `:psd`, `:jp2`, `:pnm`
7169
"""
70+
alias ExImageInfo.Types.BMP
71+
alias ExImageInfo.Types.GIF
72+
alias ExImageInfo.Types.ICO
73+
alias ExImageInfo.Types.JP2
74+
alias ExImageInfo.Types.JPEG
75+
alias ExImageInfo.Types.PNG
76+
alias ExImageInfo.Types.PNM
77+
alias ExImageInfo.Types.PSD
78+
alias ExImageInfo.Types.TIFF
79+
alias ExImageInfo.Types.WEBP
7280

7381
# Guessing function ordered by global usage
7482
# https://w3techs.com/technologies/overview/image_format/all

lib/ex_image_info/types/pnm.ex

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule ExImageInfo.Types.PNM do
2828

2929
def info(<<@signature, char::size(8), split, rest::binary>>)
3030
when split in [@nl, @space, @sharp] do
31-
with type <- signature_pnm(char),
31+
with type when not is_nil(type) <- signature_pnm(char),
3232
{w, h} <- parse(rest, nil) do
3333
{@mime, String.to_integer(w), String.to_integer(h), type}
3434
else
@@ -61,30 +61,34 @@ defmodule ExImageInfo.Types.PNM do
6161

6262
defp parse(rest, pre) do
6363
# no global
64-
with [line, next] <- :binary.split(rest, "\n"),
65-
# comments
66-
valid <- hd(:binary.split(line, "#")),
67-
ret <- Regex.run(~r/^\s*(\d+)(?:[\s]|)(?:(\d+)(?:[\s]|))?/, valid) do
68-
case ret do
69-
[_, w, h] ->
70-
if pre != nil do
71-
{pre, w}
72-
else
73-
{w, h}
74-
end
75-
76-
[_, w] ->
77-
if pre != nil do
78-
{pre, w}
79-
else
80-
parse(next, w)
81-
end
82-
83-
_ ->
84-
parse(next, pre)
85-
end
86-
else
87-
_ -> nil
64+
case :binary.split(rest, "\n") do
65+
[line, next] ->
66+
# comments
67+
valid = hd(:binary.split(line, "#"))
68+
ret = Regex.run(~r/^\s*(\d+)(?:[\s]|)(?:(\d+)(?:[\s]|))?/, valid)
69+
70+
case ret do
71+
[_, w, h] ->
72+
if pre == nil do
73+
{w, h}
74+
else
75+
{pre, w}
76+
end
77+
78+
[_, w] ->
79+
# credo:disable-for-next-line Credo.Check.Refactor.Nesting
80+
if pre == nil do
81+
parse(next, w)
82+
else
83+
{pre, w}
84+
end
85+
86+
_ ->
87+
parse(next, pre)
88+
end
89+
90+
_ ->
91+
nil
8892
end
8993
end
9094
end

lib/ex_image_info/types/tiff.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ defmodule ExImageInfo.Types.TIFF do
5959
ftype = if b_e == false, do: @ftype_ii, else: @ftype_mm
6060
w = Map.get(tags, 256)
6161
h = Map.get(tags, 257)
62-
if w != nil and h != nil, do: {@mime, w, h, ftype}, else: nil
62+
if w != nil and h != nil, do: {@mime, w, h, ftype}
6363

6464
_ ->
6565
nil

mix.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule ExImageInfo.Mixfile do
66
app: :ex_image_info,
77
description:
88
"ExImageInfo is an Elixir library to parse images (binaries) and get the dimensions (size), detected mime-type and overall validity for a set of image formats. It is the fastest and supports multiple formats.",
9-
version: File.read!("VERSION") |> String.trim(),
9+
version: "VERSION" |> File.read!() |> String.trim(),
1010
elixir: "~> 1.3",
1111
name: "ExImageInfo",
1212
package: package(),
@@ -35,7 +35,8 @@ defmodule ExImageInfo.Mixfile do
3535
{:excoveralls, "~> 0.18", only: :test},
3636
{:ex_doc, "~> 0.30", only: :dev},
3737
{:inch_ex, "~> 2.0", only: [:dev, :test]},
38-
{:credo, "~> 1.7", only: [:dev, :test], runtime: false}
38+
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
39+
{:styler, "~> 1.3", only: [:dev, :test], runtime: false}
3940
]
4041
end
4142

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
1212
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
1313
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
14+
"styler": {:hex, :styler, "1.3.2", "895910c05f26447db1aec83cfc84868b4793ce4ded74646cbdacb6600cad65f6", [:mix], [], "hexpm", "4bc3577b44f8dfd828fe323852a6c0f6a47a477e65e29ba29af1c5a7f1b54432"},
1415
}

test/ex_image_info_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule ExImageInfoTest do
22
use ExUnit.Case
3+
34
doctest ExImageInfo
45
end

test/ex_image_info_test/images/bmp_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule ExImageInfoTest.Images.BMPTest do
22
use ExUnit.Case, async: true
3+
34
import ExImageInfo
45

56
setup_all do

test/ex_image_info_test/images/gif_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule ExImageInfoTest.Images.GIFTest do
22
use ExUnit.Case, async: true
3+
34
import ExImageInfo
45

56
setup_all do

0 commit comments

Comments
 (0)