Skip to content
New issue

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

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

Already on GitHub? # to your account

Replace supported image extension list with image detection via MagickImageInfo. #818

Merged
merged 7 commits into from
May 14, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using ImageMagick;
using ImageMagick.Formats.Dng;
using ImageMagick.Formats;
using QuickLook.Common.Helpers;

namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
Expand Down
33 changes: 25 additions & 8 deletions QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Collections.Generic;
using System.IO;
using System.Windows;
using ImageMagick;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
Expand All @@ -27,19 +28,16 @@ namespace QuickLook.Plugin.ImageViewer
{
public class Plugin : IViewer
{
private static readonly HashSet<string> Formats = new HashSet<string>(new[]
private static readonly HashSet<string> WellKnownImageExtensions = new HashSet<string>(new[]
{
".apng", ".ari", ".arw", ".avif", ".bay", ".bmp", ".cap", ".cr2", ".cr3", ".crw", ".dcr", ".dcs", ".dng",
".drf", ".eip", ".emf", ".erf", ".exr", ".fff", ".gif", ".hdr", ".heic", ".heif", ".ico", ".icon", ".iiq",
".jfif", ".jpeg", ".jpg", ".k25", ".kdc", ".mdc", ".mef", ".mos", ".mrw", ".nef", ".nrw", ".obm", ".orf",
".pbm", ".pef", ".pgm", ".png", ".pnm", ".ppm", ".psd", ".ptx", ".pxn", ".r3d", ".raf", ".raw", ".rw2",
".rwl", ".rwz", ".sr2", ".srf", ".srw", ".svg", ".tga", ".tif", ".tiff", ".wdp", ".webp", ".wmf", ".x3f"
".apng", ".bmp", ".gif", ".ico", ".icon", ".jfif", ".jpeg", ".jpg", ".png", ".psd",
".svg", ".tga", ".tif", ".tiff", ".webp", ".wmf",
});

private ImagePanel _ip;
private MetaProvider _meta;

public int Priority => 0;
public int Priority => -4;

public void Init()
{
Expand All @@ -57,9 +55,28 @@ public void Init()
typeof(ImageMagickProvider)));
}

private bool IsWellKnownImageExtension(string path)
{
return WellKnownImageExtensions.Contains(Path.GetExtension(path.ToLower()));
}

private bool IsImageMagickSupported(string path)
{
try
{
return new MagickImageInfo(path).Format != MagickFormat.Unknown;
}
catch
{
return false;
}
}

public bool CanHandle(string path)
{
return !Directory.Exists(path) && Formats.Contains(Path.GetExtension(path.ToLower()));
// Only check extension for well known image and animated image types.
// For other image formats, let ImageMagick try to detect by file content.
return !Directory.Exists(path) && (IsWellKnownImageExtension(path) || IsImageMagickSupported(path));
}

public void Prepare(string path, ContextObject context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
<Reference Include="LibAPNG">
<HintPath>.\LibAPNG.dll</HintPath>
</Reference>
<Reference Include="Magick.NET-Q8-AnyCPU, Version=7.20.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>..\..\packages\Magick.NET-Q8-AnyCPU.7.20.0.1\lib\net40\Magick.NET-Q8-AnyCPU.dll</HintPath>
<Reference Include="Magick.NET-Q8-AnyCPU, Version=7.23.4.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>..\..\packages\Magick.NET-Q8-AnyCPU.7.23.4\lib\net40\Magick.NET-Q8-AnyCPU.dll</HintPath>
</Reference>
<Reference Include="Magick.NET.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>..\..\packages\Magick.NET.Core.3.0.0\lib\net40\Magick.NET.Core.dll</HintPath>
<Reference Include="Magick.NET.Core, Version=6.2.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>..\..\packages\Magick.NET.Core.6.2.0\lib\net40\Magick.NET.Core.dll</HintPath>
</Reference>
<Reference Include="Magick.NET.SystemWindowsMedia, Version=1.0.2.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>..\..\packages\Magick.NET.SystemWindowsMedia.1.0.2\lib\net40\Magick.NET.SystemWindowsMedia.dll</HintPath>
<Reference Include="Magick.NET.SystemWindowsMedia, Version=3.0.9.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>..\..\packages\Magick.NET.SystemWindowsMedia.3.0.9\lib\net40\Magick.NET.SystemWindowsMedia.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
Expand Down
6 changes: 3 additions & 3 deletions QuickLook.Plugin/QuickLook.Plugin.ImageViewer/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Magick.NET.Core" version="3.0.0" targetFramework="net462" />
<package id="Magick.NET.SystemWindowsMedia" version="1.0.2" targetFramework="net462" />
<package id="Magick.NET-Q8-AnyCPU" version="7.20.0.1" targetFramework="net462" />
<package id="Magick.NET.Core" version="6.2.0" targetFramework="net462" />
<package id="Magick.NET.SystemWindowsMedia" version="3.0.9" targetFramework="net462" />
<package id="Magick.NET-Q8-AnyCPU" version="7.23.4" targetFramework="net462" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net462" />
</packages>
89 changes: 46 additions & 43 deletions QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,19 @@ namespace QuickLook.Plugin.VideoViewer
{
public class Plugin : IViewer
{
private static readonly HashSet<string> Formats = new HashSet<string>(new[]
{
// video
".3g2", ".3gp", ".3gp2", ".3gpp", ".amv", ".asf", ".avi", ".flv", ".m4v", ".mkv", ".mov", ".mp4", ".mp4v",
".mpeg", ".mpg", ".mts", ".m2ts", ".mxf", ".ogv", ".qt", ".tp", ".ts", ".vob", ".webm", ".wmv",
// audio
".3gp", ".aa", ".aac", ".aax", ".act", ".aif", ".aiff", ".amr", ".ape", ".au", ".awb", ".dct", ".dss", ".dvf",
".flac", ".gsm", ".iklax", ".ivs", ".m4a", ".m4b", ".m4p", ".m4r", ".mka", ".mmf", ".mp3", ".mpc", ".msv",
".ogg", ".oga", ".mogg", ".opus", ".ra", ".raw", ".rm", ".tta", ".vox", ".wav", ".webm", ".wma", ".wv"
});

private ContextObject _context;
private MediaInfo.MediaInfo _mediaInfo;
private static MediaInfo.MediaInfo _mediaInfo;

private ViewerPanel _vp;

public int Priority => -10; // make it lower than TextViewer
public int Priority => -3;

static Plugin()
{
_mediaInfo = new MediaInfo.MediaInfo(Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
Environment.Is64BitProcess ? "MediaInfo-x64\\" : "MediaInfo-x86\\"));
_mediaInfo.Option("Cover_Data", "base64");
}

public void Init()
{
Expand All @@ -52,36 +48,46 @@ public void Init()

public bool CanHandle(string path)
{
return !Directory.Exists(path) && Formats.Contains(Path.GetExtension(path)?.ToLower());
if (!Directory.Exists(path))
{
try
{
_mediaInfo.Open(path);
string videoCodec = _mediaInfo.Get(StreamKind.Video, 0, "Format");
string audioCodec = _mediaInfo.Get(StreamKind.Audio, 0, "Format");
// Note MediaInfo.Close seems to close the dll and you have to re-create the MediaInfo
// object like in the static class constructor above. Any call to Get methods etc.
// will result in a "Unable to load MediaInfo library" error.
// Ref: https://github.com/MediaArea/MediaInfoLib/blob/master/Source/MediaInfoDLL/MediaInfoDLL.cs
// Pretty sure it doesn't leak when opening another file as the c++ code calls Close on Open
// Ref: https://github.com/MediaArea/MediaInfoLib/blob/master/Source/MediaInfo/MediaInfo_Internal.cpp
if (videoCodec == "Unable to load MediaInfo library") // should not happen
{
return false;
}
if (!string.IsNullOrWhiteSpace(videoCodec) || !string.IsNullOrWhiteSpace(audioCodec))
{
return true;
}
}
catch
{
// return false;
}
}

return false;
}

public void Prepare(string path, ContextObject context)
{
_context = context;

try
string videoCodec = _mediaInfo.Get(StreamKind.Video, 0, "Format");
if (!string.IsNullOrWhiteSpace(videoCodec)) // video
{
_mediaInfo = new MediaInfo.MediaInfo(Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
Environment.Is64BitProcess ? "MediaInfo-x64\\" : "MediaInfo-x86\\"));
_mediaInfo.Option("Cover_Data", "base64");
int.TryParse(_mediaInfo.Get(StreamKind.Video, 0, "Width"), out var width);
int.TryParse(_mediaInfo.Get(StreamKind.Video, 0, "Height"), out var height);
double.TryParse(_mediaInfo.Get(StreamKind.Video, 0, "Rotation"), out var rotation);

_mediaInfo.Open(path);
}
catch (Exception)
{
_mediaInfo?.Dispose();
_mediaInfo = null;
}

context.TitlebarOverlap = true;

if (_mediaInfo == null ||
!string.IsNullOrWhiteSpace(_mediaInfo.Get(StreamKind.General, 0, "VideoCount"))) // video
{
int.TryParse(_mediaInfo?.Get(StreamKind.Video, 0, "Width"), out var width);
int.TryParse(_mediaInfo?.Get(StreamKind.Video, 0, "Height"), out var height);
double.TryParse(_mediaInfo?.Get(StreamKind.Video, 0, "Rotation"), out var rotation);
// Correct rotation: on some machine the value "90" becomes "90000" by some reason
if (rotation > 360)
rotation /= 1e3;
Expand Down Expand Up @@ -110,6 +116,8 @@ public void Prepare(string path, ContextObject context)
context.TitlebarBlurVisibility = false;
context.TitlebarColourVisibility = false;
}

context.TitlebarOverlap = true;
}

public void View(string path, ContextObject context)
Expand All @@ -127,11 +135,6 @@ public void Cleanup()
{
_vp?.Dispose();
_vp = null;

_mediaInfo?.Dispose();
_mediaInfo = null;

_context = null;
}
}
}