Files
QuickLook/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs
2017-07-24 23:05:17 +03:00

111 lines
3.7 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 © 2017 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.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using ImageMagick;
using XamlAnimatedGif;
namespace QuickLook.Plugin.ImageViewer
{
public class Plugin : IViewer
{
private static readonly string[] _formats =
{
// camera raw
".3fr", ".ari", ".arw", ".bay", ".crw", ".cr2", ".cap", ".data", ".dcs", ".dcr", ".dng", ".drf", ".eip",
".erf", ".fff", ".gpr", ".iiq", ".k25", ".kdc", ".mdc", ".mef", ".mos", ".mrw", ".nef", ".nrw", ".obm",
".orf", ".pef", ".ptx", ".pxn", ".r3d", ".raf", ".raw", ".rwl", ".rw2", ".rwz", ".sr2", ".srf", ".srw",
".tif", ".x3f",
// normal
".bmp", ".gif", ".ico", ".icon", ".jpg", ".jpeg", ".png", ".psd", ".svg", ".wdp", ".tiff", ".tga", ".webp"
};
private Size _imageSize;
private ImagePanel _ip;
public int Priority => int.MaxValue;
public bool AllowsTransparency => true;
public void Init()
{
new MagickImage().Dispose();
}
public bool CanHandle(string path)
{
if (Directory.Exists(path))
return false;
return _formats.Contains(Path.GetExtension(path).ToLower());
}
public void Prepare(string path, ContextObject context)
{
// set dcraw.exe for Magick.NET
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
_imageSize = ImageFileHelper.GetImageSize(path) ?? Size.Empty;
if (!_imageSize.IsEmpty)
context.SetPreferredSizeFit(_imageSize, 0.8);
else
context.PreferredSize = new Size(1024, 768);
}
public void View(string path, ContextObject context)
{
_ip = new ImagePanel();
context.ViewerContent = _ip;
context.Title = _imageSize.IsEmpty
? $"{Path.GetFileName(path)}"
: $"{Path.GetFileName(path)} ({_imageSize.Width}×{_imageSize.Height})";
LoadImage(_ip, path);
context.IsBusy = false;
}
public void Cleanup()
{
Directory.SetCurrentDirectory(App.AppPath);
_ip = null;
}
private void LoadImage(ImagePanel ui, string path)
{
if (Path.GetExtension(path).ToLower() == ".gif")
AnimationBehavior.SetSourceUri(ui.viewPanelImage, new Uri(path));
using (var image = new MagickImage(path))
{
image.Rotate(image.Orientation == OrientationType.RightTop
? 90
: image.Orientation == OrientationType.BottomRight
? 180
: image.Orientation == OrientationType.LeftBotom
? 270
: 0);
ui.Source = image.ToBitmapSource();
}
}
}
}