diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImageFileHelper.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImageFileHelper.cs index 2050c4c..9f8933b 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImageFileHelper.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImageFileHelper.cs @@ -1,53 +1,48 @@ -using System.IO; +using System; using System.Windows; -using System.Windows.Media.Imaging; +using ExifLib; +using ImageMagick; namespace QuickLook.Plugin.ImageViewer { internal static class ImageFileHelper { - internal static Size GetImageSize(string path) + internal static Size? GetImageSize(string path) { var ori = GetOrientationFromExif(path); - using (var stream = File.OpenRead(path)) + try { - var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); - var frame = decoder.Frames[0]; + var info = new MagickImageInfo(path); - if (ori == ExifOrientation.Rotate90CW || ori == ExifOrientation.Rotate270CW) - return new Size {Width = frame.PixelHeight, Height = frame.PixelWidth}; - - return new Size {Width = frame.PixelWidth, Height = frame.PixelHeight}; + if (ori == OrientationType.RightTop || ori == OrientationType.LeftBotom) + return new Size {Width = info.Height, Height = info.Width}; + return new Size {Width = info.Width, Height = info.Height}; + } + catch (MagickException) + { + return null; } } - internal static ExifOrientation GetOrientationFromExif(string path) + private static OrientationType GetOrientationFromExif(string path) { - using (var stream = File.OpenRead(path)) + try { - var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); - var frame = decoder.Frames[0]; + using (var re = new ExifReader(path)) + { + re.GetTagValue(ExifTags.Orientation, out ushort orientation); - var orientation = ((BitmapMetadata) frame.Metadata)?.GetQuery(@"/app1/{ushort=0}/{ushort=274}"); + if (orientation == 0) + return OrientationType.Undefined; - if (orientation == null) - return ExifOrientation.Horizontal; - - return (ExifOrientation) (ushort) orientation; + return (OrientationType) orientation; + } + } + catch (Exception) + { + return OrientationType.Undefined; } - } - - internal enum ExifOrientation - { - Horizontal = 1, - MirrorHorizontal = 2, - Rotate180 = 3, - MirrorVertical = 4, - MirrorHorizontal270CW = 5, - Rotate90CW = 6, - MirrorHorizontal90CW = 7, - Rotate270CW = 8 } } } \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs index ff1ed0a..d4b5970 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/ImagePanel.xaml.cs @@ -4,7 +4,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; -using System.Windows.Media.Imaging; +using ImageMagick; using XamlAnimatedGif; namespace QuickLook.Plugin.ImageViewer @@ -30,37 +30,28 @@ namespace QuickLook.Plugin.ImageViewer viewPanel.PreviewMouseLeftButtonDown += ViewPanel_PreviewMouseLeftButtonDown; viewPanel.PreviewMouseMove += ViewPanel_PreviewMouseMove; - - viewPanel.TouchDown += ViewPanel_TouchDown; - } - - private void ViewPanel_TouchDown(object sender, TouchEventArgs e) - { - // TODO: touch support } private void LoadImage(string path) { - var ori = ImageFileHelper.GetOrientationFromExif(path); - - var bitmap = new BitmapImage(); - using (var fs = File.OpenRead(path)) + if (Path.GetExtension(path).ToLower() == ".gif") { - bitmap.BeginInit(); - bitmap.StreamSource = fs; - bitmap.CacheOption = BitmapCacheOption.OnLoad; - bitmap.Rotation = ori == ImageFileHelper.ExifOrientation.Rotate90CW - ? Rotation.Rotate90 - : ori == ImageFileHelper.ExifOrientation.Rotate270CW - ? Rotation.Rotate270 - : Rotation.Rotate0; - bitmap.EndInit(); + AnimationBehavior.SetSourceUri(viewPanelImage, new Uri(path)); + return; } - viewPanelImage.Source = bitmap; + using (var image = new MagickImage(path)) + { + image.Rotate(image.Orientation == OrientationType.RightTop + ? 90 + : image.Orientation == OrientationType.BottomRight + ? 180 + : image.Orientation == OrientationType.LeftBotom + ? 270 + : 0); - if (Path.GetExtension(path).ToLower() == ".gif") - AnimationBehavior.SetSourceUri(viewPanelImage, new Uri(path)); + viewPanelImage.Source = image.ToBitmapSource(); + } } private void ViewPanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs index 0f229e1..28418b2 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs @@ -1,10 +1,22 @@ using System.IO; +using System.Linq; +using System.Reflection; using System.Windows; 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", ".jpg", ".jpeg", ".png", ".psd", ".svg", ".wdp", ".tiff", ".tga" + }; private Size _imageSize; private ImagePanel _ip; @@ -13,47 +25,39 @@ namespace QuickLook.Plugin.ImageViewer public bool CanHandle(string path) { - // TODO: determine file type by content - if (Directory.Exists(path)) return false; - switch (Path.GetExtension(path).ToLower()) - { - case ".bmp": - case ".gif": - case ".ico": - case ".jpg": - case ".jpeg": - case ".png": - case ".wdp": - case ".tiff": - return true; - - default: - return false; - } + return _formats.Contains(Path.GetExtension(path).ToLower()); } public void Prepare(string path, ContextObject context) { - _imageSize = ImageFileHelper.GetImageSize(path); + // ImageMagick want to have dcraw.exe + Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); - context.SetPreferredSizeFit(_imageSize, 0.8); + _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(path); - context.ViewerContent = _ip; - context.Title = $"{Path.GetFileName(path)} ({_imageSize.Width}×{_imageSize.Height})"; + context.Title = _imageSize.IsEmpty + ? $"{Path.GetFileName(path)}" + : $"{Path.GetFileName(path)} ({_imageSize.Width}×{_imageSize.Height})"; context.IsBusy = false; } public void Cleanup() { + Directory.SetCurrentDirectory(App.AppPath); _ip = null; } } diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/QuickLook.Plugin.ImageViewer.csproj b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/QuickLook.Plugin.ImageViewer.csproj index 1fb8f25..6f3903d 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/QuickLook.Plugin.ImageViewer.csproj +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/QuickLook.Plugin.ImageViewer.csproj @@ -12,6 +12,8 @@ v4.6.2 512 + + true @@ -32,10 +34,17 @@ MinimumRecommendedRules.ruleset + + ..\..\packages\ExifLib.1.7.0.0\lib\net45\ExifLib.dll + + + ..\..\packages\Magick.NET-Q8-x86.7.0.5.900\lib\net40-client\Magick.NET-Q8-x86.dll + + @@ -69,5 +78,17 @@ + + + Always + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/dcraw.exe b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/dcraw.exe new file mode 100644 index 0000000..0ea3ba9 Binary files /dev/null and b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/dcraw.exe differ diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/packages.config b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/packages.config index a0bf8b3..94a91d8 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/packages.config +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/packages.config @@ -1,5 +1,7 @@  - + + + \ No newline at end of file