Files
QuickLook/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs
Frank Becker 3ef980bb17 Replace supported image extension list with image detection via MagickImageInfo. (#818)
* Replace supported image extension list with image detection via MagickImageInfo.

* Change ImageViewer priority to -4.
Change VideoViewer priority to -3 and detect audio/video via MediaInfo instead of file extensions.

* Make mediaInfo a class static and initialize once.
Add some notes about MediaInfo Open and Close.

* Remove try/catch from Prepare and let it be handled in caller.
If there was an exception due to MediaInfo it would have already occurred in CanHandle.

* Upgrade ImageMagick to latest

* Only check extension for well known image and animated image types.
For other image formats, let ImageMagick try to detect by file content.
Upgrade to latest Magick.NET

Co-authored-by: Frank Becker <frank.becker@thoughtexchange.com>
2021-05-14 19:07:32 +02:00

116 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright © 2018 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
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;
namespace QuickLook.Plugin.ImageViewer
{
public class Plugin : IViewer
{
private static readonly HashSet<string> WellKnownImageExtensions = new HashSet<string>(new[]
{
".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 => -4;
public void Init()
{
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(new[] {".apng", ".png"},
typeof(APngProvider)));
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(new[] {".gif"},
typeof(GifProvider)));
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(new[] {".bmp", ".jpg", ".jpeg", ".jfif", ".tif", ".tiff"},
typeof(NativeProvider)));
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(new[] {"*"},
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)
{
// 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)
{
_meta = new MetaProvider(path);
var size = _meta.GetSize();
if (!size.IsEmpty)
context.SetPreferredSizeFit(size, 0.8);
else
context.PreferredSize = new Size(800, 600);
context.Theme = (Themes) SettingHelper.Get("LastTheme", 1, "QuickLook.Plugin.ImageViewer");
}
public void View(string path, ContextObject context)
{
_ip = new ImagePanel(context, _meta);
var size = _meta.GetSize();
context.ViewerContent = _ip;
context.Title = size.IsEmpty
? $"{Path.GetFileName(path)}"
: $"{size.Width}×{size.Height}: {Path.GetFileName(path)}";
_ip.ImageUriSource = Helper.FilePathToFileUrl(path);
}
public void Cleanup()
{
_ip?.Dispose();
_ip = null;
}
}
}