2024-06-02 06:15:09 +02:00
|
|
|
defmodule PhilomenaMedia.Processors.Svg do
|
|
|
|
@moduledoc false
|
2019-11-26 01:06:40 +01:00
|
|
|
|
2024-06-02 06:15:09 +02:00
|
|
|
alias PhilomenaMedia.Intensities
|
|
|
|
alias PhilomenaMedia.Analyzers.Result
|
|
|
|
alias PhilomenaMedia.Processors.Processor
|
|
|
|
alias PhilomenaMedia.Processors
|
|
|
|
|
|
|
|
@behaviour Processor
|
|
|
|
|
|
|
|
@spec versions(Processors.version_list()) :: [Processors.version_filename()]
|
2022-02-08 04:12:40 +01:00
|
|
|
def versions(sizes) do
|
|
|
|
sizes
|
|
|
|
|> Enum.map(fn {name, _} -> "#{name}.png" end)
|
|
|
|
|> Kernel.++(["rendered.png", "full.png"])
|
|
|
|
end
|
|
|
|
|
2024-06-02 06:15:09 +02:00
|
|
|
@spec process(Result.t(), Path.t(), Processors.version_list()) :: Processors.edit_script()
|
2022-02-08 04:30:39 +01:00
|
|
|
def process(_analysis, file, versions) do
|
2019-11-26 03:57:47 +01:00
|
|
|
preview = preview(file)
|
2019-11-26 01:06:40 +01:00
|
|
|
|
2019-11-26 03:57:47 +01:00
|
|
|
{:ok, intensities} = Intensities.file(preview)
|
|
|
|
|
2022-02-08 04:30:39 +01:00
|
|
|
scaled = Enum.flat_map(versions, &scale(preview, &1))
|
|
|
|
full = [{:copy, preview, "full.png"}]
|
2019-11-26 01:06:40 +01:00
|
|
|
|
2024-06-02 06:15:09 +02:00
|
|
|
[
|
2019-11-26 03:57:47 +01:00
|
|
|
intensities: intensities,
|
2022-02-08 04:30:39 +01:00
|
|
|
thumbnails: scaled ++ full ++ [{:copy, preview, "rendered.png"}]
|
2024-06-02 06:15:09 +02:00
|
|
|
]
|
2019-11-26 01:06:40 +01:00
|
|
|
end
|
|
|
|
|
2024-06-02 06:15:09 +02:00
|
|
|
@spec post_process(Result.t(), Path.t()) :: Processors.edit_script()
|
|
|
|
def post_process(_analysis, _file), do: []
|
2019-11-26 03:57:47 +01:00
|
|
|
|
2024-06-02 06:15:09 +02:00
|
|
|
@spec intensities(Result.t(), Path.t()) :: Intensities.t()
|
2019-11-29 01:11:05 +01:00
|
|
|
def intensities(_analysis, file) do
|
|
|
|
{:ok, intensities} = Intensities.file(preview(file))
|
|
|
|
intensities
|
|
|
|
end
|
|
|
|
|
2019-11-26 03:57:47 +01:00
|
|
|
defp preview(file) do
|
2019-11-26 01:06:40 +01:00
|
|
|
preview = Briefly.create!(extname: ".png")
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
{_output, 0} = System.cmd("safe-rsvg-convert", [file, preview])
|
2019-11-26 01:06:40 +01:00
|
|
|
|
2019-11-26 03:57:47 +01:00
|
|
|
preview
|
2019-11-26 01:06:40 +01:00
|
|
|
end
|
|
|
|
|
2022-02-08 04:30:39 +01:00
|
|
|
defp scale(preview, {thumb_name, {width, height}}) do
|
2019-11-26 01:06:40 +01:00
|
|
|
scaled = Briefly.create!(extname: ".png")
|
|
|
|
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
|
|
|
|
|
|
|
|
{_output, 0} =
|
2019-11-27 02:45:57 +01:00
|
|
|
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", preview, "-vf", scale_filter, scaled])
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
{_output, 0} = System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled])
|
2019-11-26 01:06:40 +01:00
|
|
|
|
2022-02-08 04:30:39 +01:00
|
|
|
[{:copy, scaled, "#{thumb_name}.png"}]
|
2019-11-26 01:06:40 +01:00
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|