Code Cleanup

This commit is contained in:
ema
2024-12-11 22:17:26 +08:00
parent c056438c58
commit 28ec7655f8
89 changed files with 7796 additions and 7698 deletions

View File

@@ -1,166 +1,166 @@
// 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 QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Plugin;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
namespace QuickLook.Plugin.ImageViewer.AnimatedImage;
public class AnimatedImage : Image, IDisposable
{
public class AnimatedImage : Image, IDisposable
// List<Pair<formats, type>>
public static List<KeyValuePair<string[], Type>> Providers = new List<KeyValuePair<string[], Type>>();
private AnimationProvider _animation;
private bool _disposing;
public void Dispose()
{
// List<Pair<formats, type>>
public static List<KeyValuePair<string[], Type>> Providers = new List<KeyValuePair<string[], Type>>();
_disposing = true;
private AnimationProvider _animation;
private bool _disposing;
BeginAnimation(AnimationFrameIndexProperty, null);
Source = null;
public void Dispose()
_animation?.Dispose();
_animation = null;
}
public event EventHandler ImageLoaded;
public event EventHandler DoZoomToFit;
private static AnimationProvider InitAnimationProvider(Uri path, MetaProvider meta, ContextObject contextObject)
{
var ext = Path.GetExtension(path.LocalPath).ToLower();
var type = Providers.First(p => p.Key.Contains(ext) || p.Key.Contains("*")).Value;
var provider = type.CreateInstance<AnimationProvider>(path, meta, contextObject);
return provider;
}
#region DependencyProperty
public static readonly DependencyProperty AnimationFrameIndexProperty =
DependencyProperty.Register("AnimationFrameIndex", typeof(int), typeof(AnimatedImage),
new UIPropertyMetadata(-1, AnimationFrameIndexChanged));
public static readonly DependencyProperty AnimationUriProperty =
DependencyProperty.Register("AnimationUri", typeof(Uri), typeof(AnimatedImage),
new UIPropertyMetadata(null, AnimationUriChanged));
public static readonly DependencyProperty MetaProperty =
DependencyProperty.Register("Meta", typeof(MetaProvider), typeof(AnimatedImage));
public static readonly DependencyProperty ContextObjectProperty =
DependencyProperty.Register("ContextObject", typeof(ContextObject), typeof(AnimatedImage));
public int AnimationFrameIndex
{
get => (int)GetValue(AnimationFrameIndexProperty);
set => SetValue(AnimationFrameIndexProperty, value);
}
public Uri AnimationUri
{
get => (Uri)GetValue(AnimationUriProperty);
set => SetValue(AnimationUriProperty, value);
}
public MetaProvider Meta
{
private get => (MetaProvider)GetValue(MetaProperty);
set => SetValue(MetaProperty, value);
}
public ContextObject ContextObject
{
private get => (ContextObject)GetValue(ContextObjectProperty);
set => SetValue(ContextObjectProperty, value);
}
private static void AnimationUriChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
if (!(obj is AnimatedImage instance))
return;
//var thumbnail = instance.Meta?.GetThumbnail(true);
//instance.Source = thumbnail;
instance._animation = InitAnimationProvider((Uri)ev.NewValue, instance.Meta, instance.ContextObject);
ShowThumbnailAndStartAnimation(instance);
}
private static void ShowThumbnailAndStartAnimation(AnimatedImage instance)
{
var task = instance._animation.GetThumbnail(instance.ContextObject.PreferredSize);
if (task == null) return;
task.ContinueWith(_ => instance.Dispatcher.Invoke(() =>
{
_disposing = true;
BeginAnimation(AnimationFrameIndexProperty, null);
Source = null;
_animation?.Dispose();
_animation = null;
}
public event EventHandler ImageLoaded;
public event EventHandler DoZoomToFit;
private static AnimationProvider InitAnimationProvider(Uri path, MetaProvider meta, ContextObject contextObject)
{
var ext = Path.GetExtension(path.LocalPath).ToLower();
var type = Providers.First(p => p.Key.Contains(ext) || p.Key.Contains("*")).Value;
var provider = type.CreateInstance<AnimationProvider>(path, meta, contextObject);
return provider;
}
#region DependencyProperty
public static readonly DependencyProperty AnimationFrameIndexProperty =
DependencyProperty.Register("AnimationFrameIndex", typeof(int), typeof(AnimatedImage),
new UIPropertyMetadata(-1, AnimationFrameIndexChanged));
public static readonly DependencyProperty AnimationUriProperty =
DependencyProperty.Register("AnimationUri", typeof(Uri), typeof(AnimatedImage),
new UIPropertyMetadata(null, AnimationUriChanged));
public static readonly DependencyProperty MetaProperty =
DependencyProperty.Register("Meta", typeof(MetaProvider), typeof(AnimatedImage));
public static readonly DependencyProperty ContextObjectProperty =
DependencyProperty.Register("ContextObject", typeof(ContextObject), typeof(AnimatedImage));
public int AnimationFrameIndex
{
get => (int) GetValue(AnimationFrameIndexProperty);
set => SetValue(AnimationFrameIndexProperty, value);
}
public Uri AnimationUri
{
get => (Uri) GetValue(AnimationUriProperty);
set => SetValue(AnimationUriProperty, value);
}
public MetaProvider Meta
{
private get => (MetaProvider) GetValue(MetaProperty);
set => SetValue(MetaProperty, value);
}
public ContextObject ContextObject
{
private get => (ContextObject) GetValue(ContextObjectProperty);
set => SetValue(ContextObjectProperty, value);
}
private static void AnimationUriChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
if (!(obj is AnimatedImage instance))
return;
//var thumbnail = instance.Meta?.GetThumbnail(true);
//instance.Source = thumbnail;
instance._animation = InitAnimationProvider((Uri) ev.NewValue, instance.Meta, instance.ContextObject);
ShowThumbnailAndStartAnimation(instance);
}
private static void ShowThumbnailAndStartAnimation(AnimatedImage instance)
{
var task = instance._animation.GetThumbnail(instance.ContextObject.PreferredSize);
if (task == null) return;
task.ContinueWith(_ => instance.Dispatcher.Invoke(() =>
{
if (instance._disposing)
return;
instance.Source = _.Result;
if (_.Result != null)
{
instance.DoZoomToFit?.Invoke(instance, new EventArgs());
instance.ImageLoaded?.Invoke(instance, new EventArgs());
}
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation?.Animator);
}));
task.Start();
}
private static void AnimationFrameIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
if (!(obj is AnimatedImage instance))
return;
if (instance._disposing)
return;
var task = instance._animation.GetRenderedFrame((int) ev.NewValue);
instance.Source = _.Result;
task.ContinueWith(_ => instance.Dispatcher.Invoke(() =>
if (_.Result != null)
{
if (instance._disposing)
return;
instance.DoZoomToFit?.Invoke(instance, new EventArgs());
instance.ImageLoaded?.Invoke(instance, new EventArgs());
}
var firstLoad = instance.Source == null;
instance.Source = _.Result;
if (firstLoad)
{
instance.DoZoomToFit?.Invoke(instance, new EventArgs());
instance.ImageLoaded?.Invoke(instance, new EventArgs());
}
}));
task.Start();
}
#endregion DependencyProperty
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation?.Animator);
}));
task.Start();
}
}
private static void AnimationFrameIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
if (!(obj is AnimatedImage instance))
return;
if (instance._disposing)
return;
var task = instance._animation.GetRenderedFrame((int)ev.NewValue);
task.ContinueWith(_ => instance.Dispatcher.Invoke(() =>
{
if (instance._disposing)
return;
var firstLoad = instance.Source == null;
instance.Source = _.Result;
if (firstLoad)
{
instance.DoZoomToFit?.Invoke(instance, new EventArgs());
instance.ImageLoaded?.Invoke(instance, new EventArgs());
}
}));
task.Start();
}
#endregion DependencyProperty
}

View File

@@ -1,50 +1,49 @@
// 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 QuickLook.Common.Plugin;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
namespace QuickLook.Plugin.ImageViewer.AnimatedImage;
internal abstract class AnimationProvider : IDisposable
{
internal abstract class AnimationProvider : IDisposable
protected AnimationProvider(Uri path, MetaProvider meta, ContextObject contextObject)
{
protected AnimationProvider(Uri path, MetaProvider meta, ContextObject contextObject)
{
Path = path;
Meta = meta;
ContextObject = contextObject;
}
public Uri Path { get; }
public MetaProvider Meta { get; }
public ContextObject ContextObject { get; }
public Int32AnimationUsingKeyFrames Animator { get; protected set; }
public abstract void Dispose();
public abstract Task<BitmapSource> GetThumbnail(Size renderSize);
public abstract Task<BitmapSource> GetRenderedFrame(int index);
Path = path;
Meta = meta;
ContextObject = contextObject;
}
}
public Uri Path { get; }
public MetaProvider Meta { get; }
public ContextObject ContextObject { get; }
public Int32AnimationUsingKeyFrames Animator { get; protected set; }
public abstract void Dispose();
public abstract Task<BitmapSource> GetThumbnail(Size renderSize);
public abstract Task<BitmapSource> GetRenderedFrame(int index);
}

View File

@@ -1,20 +1,23 @@
// 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 LibAPNG;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Plugin;
using System;
using System.Collections.Generic;
using System.IO;
@@ -24,227 +27,225 @@ using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using LibAPNG;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
internal class APngProvider : AnimationProvider
{
internal class APngProvider : AnimationProvider
private readonly Frame _baseFrame;
private readonly List<FrameInfo> _frames;
private readonly List<BitmapSource> _renderedFrames;
private int _lastEffectivePreviousPreviousFrameIndex;
private NativeProvider _nativeImageProvider;
public APngProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
{
private readonly Frame _baseFrame;
private readonly List<FrameInfo> _frames;
private readonly List<BitmapSource> _renderedFrames;
private int _lastEffectivePreviousPreviousFrameIndex;
private NativeProvider _nativeImageProvider;
public APngProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
if (!IsAnimatedPng(path.LocalPath))
{
if (!IsAnimatedPng(path.LocalPath))
{
_nativeImageProvider = new NativeProvider(path, meta, contextObject);
Animator = _nativeImageProvider.Animator;
return;
}
var decoder = new APNGBitmap(path.LocalPath);
_baseFrame = decoder.DefaultImage;
_frames = new List<FrameInfo>(decoder.Frames.Length);
_renderedFrames = new List<BitmapSource>(decoder.Frames.Length);
Enumerable.Repeat(0, decoder.Frames.Length).ForEach(_ => _renderedFrames.Add(null));
Animator = new Int32AnimationUsingKeyFrames {RepeatBehavior = RepeatBehavior.Forever};
var wallclock = TimeSpan.Zero;
for (var i = 0; i < decoder.Frames.Length; i++)
{
var frame = decoder.Frames[i];
_frames.Add(new FrameInfo(decoder.IHDRChunk, frame));
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(i, KeyTime.FromTimeSpan(wallclock)));
wallclock += _frames[i].Delay;
}
_nativeImageProvider = new NativeProvider(path, meta, contextObject);
Animator = _nativeImageProvider.Animator;
return;
}
public override Task<BitmapSource> GetThumbnail(Size renderSize)
var decoder = new APNGBitmap(path.LocalPath);
_baseFrame = decoder.DefaultImage;
_frames = new List<FrameInfo>(decoder.Frames.Length);
_renderedFrames = new List<BitmapSource>(decoder.Frames.Length);
Enumerable.Repeat(0, decoder.Frames.Length).ForEach(_ => _renderedFrames.Add(null));
Animator = new Int32AnimationUsingKeyFrames { RepeatBehavior = RepeatBehavior.Forever };
var wallclock = TimeSpan.Zero;
for (var i = 0; i < decoder.Frames.Length; i++)
{
if (_nativeImageProvider != null)
return _nativeImageProvider.GetThumbnail(renderSize);
var frame = decoder.Frames[i];
return new Task<BitmapSource>(() =>
{
var bs = _baseFrame.GetBitmapSource();
_frames.Add(new FrameInfo(decoder.IHDRChunk, frame));
bs.Freeze();
return bs;
});
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
if (_nativeImageProvider != null)
return _nativeImageProvider.GetRenderedFrame(index);
if (_renderedFrames[index] != null)
return new Task<BitmapSource>(() => _renderedFrames[index]);
return new Task<BitmapSource>(() =>
{
var rendered = Render(index);
_renderedFrames[index] = rendered;
return rendered;
});
}
public override void Dispose()
{
if (_nativeImageProvider != null)
{
_nativeImageProvider.Dispose();
_nativeImageProvider = null;
return;
}
_frames.Clear();
_renderedFrames.Clear();
}
private BitmapSource Render(int index)
{
var currentFrame = _frames[index];
FrameInfo previousFrame = null;
BitmapSource previousRendered = null;
BitmapSource previousPreviousRendered = null;
if (index > 0)
{
if (_renderedFrames[index - 1] == null)
_renderedFrames[index - 1] = Render(index - 1);
previousFrame = _frames[index - 1];
previousRendered = _renderedFrames[index - 1];
}
// when saying APNGDisposeOpPrevious, we need to find the last frame not having APNGDisposeOpPrevious.
// Only [index-2] is not correct here since that frame may also have APNGDisposeOpPrevious.
if (index > 1)
previousPreviousRendered = _renderedFrames[_lastEffectivePreviousPreviousFrameIndex];
if (_frames[index].DisposeOp != DisposeOps.APNGDisposeOpPrevious)
_lastEffectivePreviousPreviousFrameIndex = Math.Max(_lastEffectivePreviousPreviousFrameIndex, index);
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
// protect region
if (currentFrame.BlendOp == BlendOps.APNGBlendOpSource)
{
var freeRegion = new CombinedGeometry(GeometryCombineMode.Xor,
new RectangleGeometry(currentFrame.FrameRect),
new RectangleGeometry(currentFrame.FrameRect));
context.PushOpacityMask(
new DrawingBrush(new GeometryDrawing(Brushes.Transparent, null, freeRegion)));
}
if (previousFrame != null)
switch (previousFrame.DisposeOp)
{
case DisposeOps.APNGDisposeOpNone:
if (previousRendered != null)
context.DrawImage(previousRendered, currentFrame.FullRect);
break;
case DisposeOps.APNGDisposeOpPrevious:
if (previousPreviousRendered != null)
context.DrawImage(previousPreviousRendered, currentFrame.FullRect);
break;
case DisposeOps.APNGDisposeOpBackground:
// do nothing
break;
}
// unprotect region and draw current frame
if (currentFrame.BlendOp == BlendOps.APNGBlendOpSource)
context.Pop();
context.DrawImage(currentFrame.Pixels, currentFrame.FrameRect);
}
var bitmap = new RenderTargetBitmap(
(int) currentFrame.FullRect.Width, (int) currentFrame.FullRect.Height,
Math.Floor(currentFrame.Pixels.DpiX), Math.Floor(currentFrame.Pixels.DpiY),
PixelFormats.Pbgra32);
bitmap.Render(visual);
bitmap.Freeze();
return bitmap;
}
private static bool IsAnimatedPng(string path)
{
using (var br = new BinaryReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
if (br.BaseStream.Length < 8 + 4)
return false;
uint nextChunk = 8; // skip header
while (nextChunk > 0 && nextChunk < br.BaseStream.Length)
{
br.BaseStream.Position = nextChunk;
var data_size = ToUInt32BE(br.ReadBytes(4)); // data size in BE
var window = br.ReadBytes(4); // label
if (window[0] == 'I' && window[1] == 'D' && window[2] == 'A' && window[3] == 'T')
return false;
if (window[0] == 'a' && window[1] == 'c' && window[2] == 'T' && window[3] == 'L')
return true;
// *[Data Size] + Data Size + Label + CRC
nextChunk += data_size + 4 + 4 + 4;
}
return false;
}
uint ToUInt32BE(byte[] data)
{
Array.Reverse(data);
return BitConverter.ToUInt32(data, 0);
}
}
private class FrameInfo
{
public readonly BlendOps BlendOp;
public readonly TimeSpan Delay;
public readonly DisposeOps DisposeOp;
public readonly Rect FrameRect;
public readonly Rect FullRect;
public readonly BitmapSource Pixels;
public FrameInfo(IHDRChunk header, Frame frame)
{
FullRect = new Rect(0, 0, header.Width, header.Height);
FrameRect = new Rect(frame.fcTLChunk.XOffset, frame.fcTLChunk.YOffset,
frame.fcTLChunk.Width, frame.fcTLChunk.Height);
BlendOp = frame.fcTLChunk.BlendOp;
DisposeOp = frame.fcTLChunk.DisposeOp;
Pixels = frame.GetBitmapSource();
Pixels.Freeze();
Delay = TimeSpan.FromSeconds((double) frame.fcTLChunk.DelayNum /
(frame.fcTLChunk.DelayDen == 0
? 100
: frame.fcTLChunk.DelayDen));
}
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(i, KeyTime.FromTimeSpan(wallclock)));
wallclock += _frames[i].Delay;
}
}
}
public override Task<BitmapSource> GetThumbnail(Size renderSize)
{
if (_nativeImageProvider != null)
return _nativeImageProvider.GetThumbnail(renderSize);
return new Task<BitmapSource>(() =>
{
var bs = _baseFrame.GetBitmapSource();
bs.Freeze();
return bs;
});
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
if (_nativeImageProvider != null)
return _nativeImageProvider.GetRenderedFrame(index);
if (_renderedFrames[index] != null)
return new Task<BitmapSource>(() => _renderedFrames[index]);
return new Task<BitmapSource>(() =>
{
var rendered = Render(index);
_renderedFrames[index] = rendered;
return rendered;
});
}
public override void Dispose()
{
if (_nativeImageProvider != null)
{
_nativeImageProvider.Dispose();
_nativeImageProvider = null;
return;
}
_frames.Clear();
_renderedFrames.Clear();
}
private BitmapSource Render(int index)
{
var currentFrame = _frames[index];
FrameInfo previousFrame = null;
BitmapSource previousRendered = null;
BitmapSource previousPreviousRendered = null;
if (index > 0)
{
if (_renderedFrames[index - 1] == null)
_renderedFrames[index - 1] = Render(index - 1);
previousFrame = _frames[index - 1];
previousRendered = _renderedFrames[index - 1];
}
// when saying APNGDisposeOpPrevious, we need to find the last frame not having APNGDisposeOpPrevious.
// Only [index-2] is not correct here since that frame may also have APNGDisposeOpPrevious.
if (index > 1)
previousPreviousRendered = _renderedFrames[_lastEffectivePreviousPreviousFrameIndex];
if (_frames[index].DisposeOp != DisposeOps.APNGDisposeOpPrevious)
_lastEffectivePreviousPreviousFrameIndex = Math.Max(_lastEffectivePreviousPreviousFrameIndex, index);
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
// protect region
if (currentFrame.BlendOp == BlendOps.APNGBlendOpSource)
{
var freeRegion = new CombinedGeometry(GeometryCombineMode.Xor,
new RectangleGeometry(currentFrame.FrameRect),
new RectangleGeometry(currentFrame.FrameRect));
context.PushOpacityMask(
new DrawingBrush(new GeometryDrawing(Brushes.Transparent, null, freeRegion)));
}
if (previousFrame != null)
switch (previousFrame.DisposeOp)
{
case DisposeOps.APNGDisposeOpNone:
if (previousRendered != null)
context.DrawImage(previousRendered, currentFrame.FullRect);
break;
case DisposeOps.APNGDisposeOpPrevious:
if (previousPreviousRendered != null)
context.DrawImage(previousPreviousRendered, currentFrame.FullRect);
break;
case DisposeOps.APNGDisposeOpBackground:
// do nothing
break;
}
// unprotect region and draw current frame
if (currentFrame.BlendOp == BlendOps.APNGBlendOpSource)
context.Pop();
context.DrawImage(currentFrame.Pixels, currentFrame.FrameRect);
}
var bitmap = new RenderTargetBitmap(
(int)currentFrame.FullRect.Width, (int)currentFrame.FullRect.Height,
Math.Floor(currentFrame.Pixels.DpiX), Math.Floor(currentFrame.Pixels.DpiY),
PixelFormats.Pbgra32);
bitmap.Render(visual);
bitmap.Freeze();
return bitmap;
}
private static bool IsAnimatedPng(string path)
{
using (var br = new BinaryReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
if (br.BaseStream.Length < 8 + 4)
return false;
uint nextChunk = 8; // skip header
while (nextChunk > 0 && nextChunk < br.BaseStream.Length)
{
br.BaseStream.Position = nextChunk;
var data_size = ToUInt32BE(br.ReadBytes(4)); // data size in BE
var window = br.ReadBytes(4); // label
if (window[0] == 'I' && window[1] == 'D' && window[2] == 'A' && window[3] == 'T')
return false;
if (window[0] == 'a' && window[1] == 'c' && window[2] == 'T' && window[3] == 'L')
return true;
// *[Data Size] + Data Size + Label + CRC
nextChunk += data_size + 4 + 4 + 4;
}
return false;
}
uint ToUInt32BE(byte[] data)
{
Array.Reverse(data);
return BitConverter.ToUInt32(data, 0);
}
}
private class FrameInfo
{
public readonly BlendOps BlendOp;
public readonly TimeSpan Delay;
public readonly DisposeOps DisposeOp;
public readonly Rect FrameRect;
public readonly Rect FullRect;
public readonly BitmapSource Pixels;
public FrameInfo(IHDRChunk header, Frame frame)
{
FullRect = new Rect(0, 0, header.Width, header.Height);
FrameRect = new Rect(frame.fcTLChunk.XOffset, frame.fcTLChunk.YOffset,
frame.fcTLChunk.Width, frame.fcTLChunk.Height);
BlendOp = frame.fcTLChunk.BlendOp;
DisposeOp = frame.fcTLChunk.DisposeOp;
Pixels = frame.GetBitmapSource();
Pixels.Freeze();
Delay = TimeSpan.FromSeconds((double)frame.fcTLChunk.DelayNum /
(frame.fcTLChunk.DelayDen == 0
? 100
: frame.fcTLChunk.DelayDen));
}
}
}

View File

@@ -1,30 +1,29 @@
// Copyright © 2020 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 QuickLook.Common.Plugin;
using System;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
internal class DcrawProvider : NativeProvider
{
internal class DcrawProvider : NativeProvider
public DcrawProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
{
public DcrawProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
{
throw new NotImplementedException();
}
throw new NotImplementedException();
}
}
}

View File

@@ -1,103 +1,102 @@
// 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 QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using Size = System.Windows.Size;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
{
internal class GifProvider : AnimationProvider
{
private Bitmap _fileHandle;
private BitmapSource _frame;
private bool _isPlaying;
private NativeProvider _nativeProvider;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
public GifProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
internal class GifProvider : AnimationProvider
{
private Bitmap _fileHandle;
private BitmapSource _frame;
private bool _isPlaying;
private NativeProvider _nativeProvider;
public GifProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
{
if (!ImageAnimator.CanAnimate(Image.FromFile(path.LocalPath)))
{
if (!ImageAnimator.CanAnimate(Image.FromFile(path.LocalPath)))
_nativeProvider = new NativeProvider(path, meta, contextObject);
return;
}
_fileHandle = (Bitmap)Image.FromFile(path.LocalPath);
_fileHandle.SetResolution(DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal,
DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical);
Animator = new Int32AnimationUsingKeyFrames { RepeatBehavior = RepeatBehavior.Forever };
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))));
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(10))));
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(2, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(20))));
}
public override void Dispose()
{
_nativeProvider?.Dispose();
_nativeProvider = null;
ImageAnimator.StopAnimate(_fileHandle, OnFrameChanged);
_fileHandle?.Dispose();
_fileHandle = null;
_frame = null;
}
public override Task<BitmapSource> GetThumbnail(Size renderSize)
{
if (_nativeProvider != null)
return _nativeProvider.GetThumbnail(renderSize);
return new Task<BitmapSource>(() =>
{
_frame = _fileHandle.ToBitmapSource();
return _frame;
});
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
if (_nativeProvider != null)
return _nativeProvider.GetRenderedFrame(index);
return new Task<BitmapSource>(() =>
{
if (!_isPlaying)
{
_nativeProvider = new NativeProvider(path, meta, contextObject);
return;
_isPlaying = true;
ImageAnimator.Animate(_fileHandle, OnFrameChanged);
}
_fileHandle = (Bitmap) Image.FromFile(path.LocalPath);
_fileHandle.SetResolution(DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal,
DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical);
Animator = new Int32AnimationUsingKeyFrames {RepeatBehavior = RepeatBehavior.Forever};
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))));
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(10))));
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(2, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(20))));
}
public override void Dispose()
{
_nativeProvider?.Dispose();
_nativeProvider = null;
ImageAnimator.StopAnimate(_fileHandle, OnFrameChanged);
_fileHandle?.Dispose();
_fileHandle = null;
_frame = null;
}
public override Task<BitmapSource> GetThumbnail(Size renderSize)
{
if (_nativeProvider != null)
return _nativeProvider.GetThumbnail(renderSize);
return new Task<BitmapSource>(() =>
{
_frame = _fileHandle.ToBitmapSource();
return _frame;
});
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
if (_nativeProvider != null)
return _nativeProvider.GetRenderedFrame(index);
return new Task<BitmapSource>(() =>
{
if (!_isPlaying)
{
_isPlaying = true;
ImageAnimator.Animate(_fileHandle, OnFrameChanged);
}
return _frame;
});
}
private void OnFrameChanged(object sender, EventArgs e)
{
ImageAnimator.UpdateFrames();
_frame = _fileHandle.ToBitmapSource();
}
return _frame;
});
}
}
private void OnFrameChanged(object sender, EventArgs e)
{
ImageAnimator.UpdateFrames();
_frame = _fileHandle.ToBitmapSource();
}
}

View File

@@ -1,169 +1,175 @@
// 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 QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
internal class NativeProvider : AnimationProvider
{
internal class NativeProvider : AnimationProvider
public NativeProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
{
public NativeProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
{
Animator = new Int32AnimationUsingKeyFrames();
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0,
KeyTime.FromTimeSpan(TimeSpan.Zero)));
}
public override Task<BitmapSource> GetThumbnail(Size renderSize)
{
var fullSize = Meta.GetSize();
//var decodeWidth = (int) Math.Round(fullSize.Width *
// Math.Min(renderSize.Width / 2 / fullSize.Width,
// renderSize.Height / 2 / fullSize.Height));
//var decodeHeight = (int) Math.Round(fullSize.Height / fullSize.Width * decodeWidth);
var decodeWidth =
(int) Math.Round(Math.Min(Meta.GetSize().Width, Math.Max(1d, Math.Floor(renderSize.Width))));
var decodeHeight =
(int) Math.Round(Math.Min(Meta.GetSize().Height, Math.Max(1d, Math.Floor(renderSize.Height))));
var orientation = Meta.GetOrientation();
var rotate = ShouldRotate(orientation);
return new Task<BitmapSource>(() =>
{
try
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = Path;
img.CacheOption = BitmapCacheOption.OnLoad;
// specific renderSize to avoid .net's double to int conversion
img.DecodePixelWidth = rotate ? decodeHeight : decodeWidth;
img.DecodePixelHeight = rotate ? decodeWidth : decodeHeight;
img.EndInit();
var scaled = rotate
? new TransformedBitmap(img,
new ScaleTransform(fullSize.Height / img.PixelWidth, fullSize.Width / img.PixelHeight))
: new TransformedBitmap(img,
new ScaleTransform(fullSize.Width / img.PixelWidth, fullSize.Height / img.PixelHeight));
var rotated = ApplyTransformFromExif(scaled, orientation);
Helper.DpiHack(rotated);
rotated.Freeze();
return rotated;
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
return null;
}
});
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
var fullSize = Meta.GetSize();
var rotate = ShouldRotate(Meta.GetOrientation());
return new Task<BitmapSource>(() =>
{
try
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = Path;
img.CacheOption = BitmapCacheOption.OnLoad;
img.DecodePixelWidth = (int) (rotate ? fullSize.Height : fullSize.Width);
img.DecodePixelHeight = (int) (rotate ? fullSize.Width : fullSize.Height);
img.EndInit();
var img2 = ApplyTransformFromExif(img, Meta.GetOrientation());
Helper.DpiHack(img2);
img2.Freeze();
return img2;
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
return null;
}
});
}
private static bool ShouldRotate(Orientation orientation)
{
var rotate = false;
switch (orientation)
{
case Orientation.LeftTop:
case Orientation.RightTop:
case Orientation.RightBottom:
case Orientation.LeftBottom:
rotate = true;
break;
}
return rotate;
}
private static BitmapSource ApplyTransformFromExif(BitmapSource image, Orientation orientation)
{
switch (orientation)
{
case Orientation.Undefined:
case Orientation.TopLeft:
return image;
case Orientation.TopRight:
return new TransformedBitmap(image, new ScaleTransform(-1, 1, 0, 0));
case Orientation.BottomRight:
return new TransformedBitmap(image, new RotateTransform(180));
case Orientation.BottomLeft:
return new TransformedBitmap(image, new ScaleTransform(1, 1, 0, 0));
case Orientation.LeftTop:
return new TransformedBitmap(
new TransformedBitmap(image, new RotateTransform(90)),
new ScaleTransform(-1, 1, 0, 0));
case Orientation.RightTop:
return new TransformedBitmap(image, new RotateTransform(90));
case Orientation.RightBottom:
return new TransformedBitmap(
new TransformedBitmap(image, new RotateTransform(270)),
new ScaleTransform(-1, 1, 0, 0));
case Orientation.LeftBottom:
return new TransformedBitmap(image, new RotateTransform(270));
}
return image;
}
public override void Dispose()
{
}
Animator = new Int32AnimationUsingKeyFrames();
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0,
KeyTime.FromTimeSpan(TimeSpan.Zero)));
}
}
public override Task<BitmapSource> GetThumbnail(Size renderSize)
{
var fullSize = Meta.GetSize();
//var decodeWidth = (int) Math.Round(fullSize.Width *
// Math.Min(renderSize.Width / 2 / fullSize.Width,
// renderSize.Height / 2 / fullSize.Height));
//var decodeHeight = (int) Math.Round(fullSize.Height / fullSize.Width * decodeWidth);
var decodeWidth =
(int)Math.Round(Math.Min(Meta.GetSize().Width, Math.Max(1d, Math.Floor(renderSize.Width))));
var decodeHeight =
(int)Math.Round(Math.Min(Meta.GetSize().Height, Math.Max(1d, Math.Floor(renderSize.Height))));
var orientation = Meta.GetOrientation();
var rotate = ShouldRotate(orientation);
return new Task<BitmapSource>(() =>
{
try
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = Path;
img.CacheOption = BitmapCacheOption.OnLoad;
// specific renderSize to avoid .net's double to int conversion
img.DecodePixelWidth = rotate ? decodeHeight : decodeWidth;
img.DecodePixelHeight = rotate ? decodeWidth : decodeHeight;
img.EndInit();
var scaled = rotate
? new TransformedBitmap(img,
new ScaleTransform(fullSize.Height / img.PixelWidth, fullSize.Width / img.PixelHeight))
: new TransformedBitmap(img,
new ScaleTransform(fullSize.Width / img.PixelWidth, fullSize.Height / img.PixelHeight));
var rotated = ApplyTransformFromExif(scaled, orientation);
Helper.DpiHack(rotated);
rotated.Freeze();
return rotated;
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
return null;
}
});
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
var fullSize = Meta.GetSize();
var rotate = ShouldRotate(Meta.GetOrientation());
return new Task<BitmapSource>(() =>
{
try
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = Path;
img.CacheOption = BitmapCacheOption.OnLoad;
img.DecodePixelWidth = (int)(rotate ? fullSize.Height : fullSize.Width);
img.DecodePixelHeight = (int)(rotate ? fullSize.Width : fullSize.Height);
img.EndInit();
var img2 = ApplyTransformFromExif(img, Meta.GetOrientation());
Helper.DpiHack(img2);
img2.Freeze();
return img2;
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
return null;
}
});
}
private static bool ShouldRotate(Orientation orientation)
{
var rotate = false;
switch (orientation)
{
case Orientation.LeftTop:
case Orientation.RightTop:
case Orientation.RightBottom:
case Orientation.LeftBottom:
rotate = true;
break;
}
return rotate;
}
private static BitmapSource ApplyTransformFromExif(BitmapSource image, Orientation orientation)
{
switch (orientation)
{
case Orientation.Undefined:
case Orientation.TopLeft:
return image;
case Orientation.TopRight:
return new TransformedBitmap(image, new ScaleTransform(-1, 1, 0, 0));
case Orientation.BottomRight:
return new TransformedBitmap(image, new RotateTransform(180));
case Orientation.BottomLeft:
return new TransformedBitmap(image, new ScaleTransform(1, 1, 0, 0));
case Orientation.LeftTop:
return new TransformedBitmap(
new TransformedBitmap(image, new RotateTransform(90)),
new ScaleTransform(-1, 1, 0, 0));
case Orientation.RightTop:
return new TransformedBitmap(image, new RotateTransform(90));
case Orientation.RightBottom:
return new TransformedBitmap(
new TransformedBitmap(image, new RotateTransform(270)),
new ScaleTransform(-1, 1, 0, 0));
case Orientation.LeftBottom:
return new TransformedBitmap(image, new RotateTransform(270));
}
return image;
}
public override void Dispose()
{
}
}

View File

@@ -1,71 +1,70 @@
// Copyright © 2020 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 QuickLook.Common.Helpers;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Media.Imaging;
using QuickLook.Common.Helpers;
namespace QuickLook.Plugin.ImageViewer
namespace QuickLook.Plugin.ImageViewer;
internal class Helper
{
internal class Helper
public static void DpiHack(BitmapSource img)
{
public static void DpiHack(BitmapSource img)
{
// a dirty hack... but is the fastest
// a dirty hack... but is the fastest
var newDpiX = (double) DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal;
var newDpiY = (double) DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical;
var newDpiX = (double)DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal;
var newDpiY = (double)DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical;
var dpiX = img.GetType().GetField("_dpiX",
BindingFlags.NonPublic | BindingFlags.Instance);
var dpiY = img.GetType().GetField("_dpiY",
BindingFlags.NonPublic | BindingFlags.Instance);
dpiX?.SetValue(img, newDpiX);
dpiY?.SetValue(img, newDpiY);
}
var dpiX = img.GetType().GetField("_dpiX",
BindingFlags.NonPublic | BindingFlags.Instance);
var dpiY = img.GetType().GetField("_dpiY",
BindingFlags.NonPublic | BindingFlags.Instance);
dpiX?.SetValue(img, newDpiX);
dpiY?.SetValue(img, newDpiY);
}
public static Uri FilePathToFileUrl(string filePath)
{
var uri = new StringBuilder();
foreach (var v in filePath)
if (v >= 'a' && v <= 'z' || v >= 'A' && v <= 'Z' || v >= '0' && v <= '9' ||
v == '+' || v == '/' || v == ':' || v == '.' || v == '-' || v == '_' || v == '~' ||
v > '\x80')
uri.Append(v);
else if (v == Path.DirectorySeparatorChar || v == Path.AltDirectorySeparatorChar)
uri.Append('/');
else
uri.Append($"%{(int) v:X2}");
if (uri.Length >= 2 && uri[0] == '/' && uri[1] == '/') // UNC path
uri.Insert(0, "file:");
public static Uri FilePathToFileUrl(string filePath)
{
var uri = new StringBuilder();
foreach (var v in filePath)
if (v >= 'a' && v <= 'z' || v >= 'A' && v <= 'Z' || v >= '0' && v <= '9' ||
v == '+' || v == '/' || v == ':' || v == '.' || v == '-' || v == '_' || v == '~' ||
v > '\x80')
uri.Append(v);
else if (v == Path.DirectorySeparatorChar || v == Path.AltDirectorySeparatorChar)
uri.Append('/');
else
uri.Insert(0, "file:///");
uri.Append($"%{(int)v:X2}");
if (uri.Length >= 2 && uri[0] == '/' && uri[1] == '/') // UNC path
uri.Insert(0, "file:");
else
uri.Insert(0, "file:///");
try
{
return new Uri(uri.ToString());
}
catch
{
return null;
}
try
{
return new Uri(uri.ToString());
}
catch
{
return null;
}
}
}
}

View File

@@ -1,48 +1,52 @@
<UserControl x:Class="QuickLook.Plugin.ImageViewer.ImagePanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:animatedImage="clr-namespace:QuickLook.Plugin.ImageViewer.AnimatedImage"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:QuickLook.Plugin.ImageViewer"
xmlns:animatedImage="clr-namespace:QuickLook.Plugin.ImageViewer.AnimatedImage"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:plugin="clr-namespace:QuickLook.Common.Plugin;assembly=QuickLook.Common"
mc:Ignorable="d"
x:Name="imagePanel"
d:DesignHeight="300" d:DesignWidth="300">
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- only for design -->
<!-- only for design -->
<ResourceDictionary Source="/QuickLook.Common;component/Styles/MainWindowStyles.xaml" />
<ResourceDictionary Source="/QuickLook.Common;component/Styles/MainWindowStyles.Dark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Rectangle Visibility="{Binding BackgroundVisibility, ElementName=imagePanel}"
RenderOptions.BitmapScalingMode="NearestNeighbor">
<Rectangle RenderOptions.BitmapScalingMode="NearestNeighbor" Visibility="{Binding BackgroundVisibility, ElementName=imagePanel}">
<Rectangle.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=imagePanel,Path=Theme}"
Value="{x:Static plugin:Themes.Dark}">
<DataTrigger Binding="{Binding ElementName=imagePanel, Path=Theme}" Value="{x:Static plugin:Themes.Dark}">
<Setter Property="Rectangle.Fill">
<Setter.Value>
<ImageBrush AlignmentY="Top" Viewport="0,0,32,32"
RenderOptions.BitmapScalingMode="NearestNeighbor"
<ImageBrush AlignmentY="Top"
ImageSource="Resources/background-b.png"
ViewportUnits="Absolute" Stretch="UniformToFill" TileMode="Tile" />
RenderOptions.BitmapScalingMode="NearestNeighbor"
Stretch="UniformToFill"
TileMode="Tile"
Viewport="0,0,32,32"
ViewportUnits="Absolute" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=imagePanel,Path=Theme}"
Value="{x:Static plugin:Themes.Light}">
<DataTrigger Binding="{Binding ElementName=imagePanel, Path=Theme}" Value="{x:Static plugin:Themes.Light}">
<Setter Property="Rectangle.Fill">
<Setter.Value>
<ImageBrush AlignmentY="Top" Viewport="0,0,32,32"
RenderOptions.BitmapScalingMode="NearestNeighbor"
<ImageBrush AlignmentY="Top"
ImageSource="Resources/background.png"
ViewportUnits="Absolute" Stretch="UniformToFill" TileMode="Tile" />
RenderOptions.BitmapScalingMode="NearestNeighbor"
Stretch="UniformToFill"
TileMode="Tile"
Viewport="0,0,32,32"
ViewportUnits="Absolute" />
</Setter.Value>
</Setter>
</DataTrigger>
@@ -50,46 +54,72 @@
</Style>
</Rectangle.Style>
</Rectangle>
<ScrollViewer x:Name="viewPanel" BorderThickness="0" HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto" Focusable="False" IsManipulationEnabled="True">
<animatedImage:AnimatedImage x:Name="viewPanelImage" Stretch="None"
Meta="{Binding Meta, ElementName=imagePanel}"
<ScrollViewer x:Name="viewPanel"
BorderThickness="0"
Focusable="False"
HorizontalScrollBarVisibility="Auto"
IsManipulationEnabled="True"
VerticalScrollBarVisibility="Auto">
<animatedImage:AnimatedImage x:Name="viewPanelImage"
AnimationUri="{Binding ImageUriSource, ElementName=imagePanel}"
ContextObject="{Binding ContextObject, ElementName=imagePanel}"
Meta="{Binding Meta, ElementName=imagePanel}"
RenderOptions.BitmapScalingMode="{Binding RenderMode, ElementName=imagePanel}"
AnimationUri="{Binding ImageUriSource, ElementName=imagePanel}" />
Stretch="None" />
</ScrollViewer>
<Border x:Name="zoomLevelInfo" CornerRadius="5" IsHitTestVisible="False" Opacity="0" Background="Gray"
Padding="15,4,15,4" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Foreground="White" FontSize="18"
<Border x:Name="zoomLevelInfo"
Padding="15,4,15,4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="Gray"
CornerRadius="5"
IsHitTestVisible="False"
Opacity="0">
<TextBlock FontSize="18"
Foreground="White"
Text="{Binding ElementName=imagePanel, Path=ZoomFactor, StringFormat={}{0:P0}}" />
<Border.Resources>
<Storyboard x:Key="StoryboardShowZoomLevelInfo">
<DoubleAnimationUsingKeyFrames Storyboard.Target="{Binding Source={x:Reference zoomLevelInfo}}"
Storyboard.TargetProperty="Opacity">
<DoubleAnimationUsingKeyFrames Storyboard.Target="{Binding Source={x:Reference zoomLevelInfo}}" Storyboard.TargetProperty="Opacity">
<DoubleAnimationUsingKeyFrames.KeyFrames>
<LinearDoubleKeyFrame Value="0.9" KeyTime="0:0:0.1" />
<LinearDoubleKeyFrame Value="0.9" KeyTime="0:0:0.6" />
<LinearDoubleKeyFrame Value="0" KeyTime="0:0:0.8" />
<LinearDoubleKeyFrame KeyTime="0:0:0.1" Value="0.9" />
<LinearDoubleKeyFrame KeyTime="0:0:0.6" Value="0.9" />
<LinearDoubleKeyFrame KeyTime="0:0:0.8" Value="0" />
</DoubleAnimationUsingKeyFrames.KeyFrames>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Border.Resources>
</Border>
<Button x:Name="buttonMeta" Width="24" Height="24" Style="{StaticResource CaptionButtonStyle}"
HorizontalAlignment="Right" VerticalAlignment="Top"
Visibility="{Binding ElementName=imagePanel, Path=MetaIconVisibility}"
Margin="0,8,40,0" Content="&#xE946;" />
<Button x:Name="buttonMeta"
Width="24"
Height="24"
Margin="0,8,40,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Content="&#xE946;"
Style="{StaticResource CaptionButtonStyle}"
Visibility="{Binding ElementName=imagePanel, Path=MetaIconVisibility}" />
<Button x:Name="buttonBackgroundColour" Style="{StaticResource CaptionButtonStyle}" Width="24" Height="24"
HorizontalAlignment="Right" VerticalAlignment="Top"
Visibility="{Binding ElementName=imagePanel, Path=BackgroundVisibility}"
Margin="0,8,8,0" Content="&#xEF1F;" />
<Button x:Name="buttonBackgroundColour"
Width="24"
Height="24"
Margin="0,8,8,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Content="&#xEF1F;"
Style="{StaticResource CaptionButtonStyle}"
Visibility="{Binding ElementName=imagePanel, Path=BackgroundVisibility}" />
<TextBlock x:Name="textMeta" IsHitTestVisible="False" HorizontalAlignment="Right"
VerticalAlignment="Top" FontSize="11"
Padding="5,5,5,5" Margin="0,40,8,0" Background="{DynamicResource CaptionBackground}"
Foreground="{DynamicResource WindowTextForeground}">
<TextBlock x:Name="textMeta"
Margin="0,40,8,0"
Padding="5,5,5,5"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Background="{DynamicResource CaptionBackground}"
FontSize="11"
Foreground="{DynamicResource WindowTextForeground}"
IsHitTestVisible="False">
<TextBlock.Inlines>
<Run FontWeight="SemiBold">Camera maker</Run>
<Run>:&#160;</Run>
@@ -101,7 +131,7 @@
<Setter Property="TextBlock.Visibility" Value="Collapsed" />
</Style.Setters>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=buttonMeta,Path=IsMouseOver}" Value="True">
<DataTrigger Binding="{Binding ElementName=buttonMeta, Path=IsMouseOver}" Value="True">
<Setter Property="TextBlock.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>

View File

@@ -1,20 +1,24 @@
// 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 QuickLook.Common.Annotations;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using System;
using System.ComponentModel;
using System.Diagnostics;
@@ -30,493 +34,489 @@ using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using QuickLook.Common.Annotations;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.ImageViewer
namespace QuickLook.Plugin.ImageViewer;
/// <summary>
/// Interaction logic for ImagePanel.xaml
/// </summary>
public partial class ImagePanel : UserControl, INotifyPropertyChanged, IDisposable
{
/// <summary>
/// Interaction logic for ImagePanel.xaml
/// </summary>
public partial class ImagePanel : UserControl, INotifyPropertyChanged, IDisposable
private Visibility _backgroundVisibility = Visibility.Visible;
private ContextObject _contextObject;
private Point? _dragInitPos;
private Uri _imageSource;
private bool _isZoomFactorFirstSet = true;
private DateTime _lastZoomTime = DateTime.MinValue;
private double _maxZoomFactor = 3d;
private MetaProvider _meta;
private Visibility _metaIconVisibility = Visibility.Visible;
private double _minZoomFactor = 0.1d;
private BitmapScalingMode _renderMode = BitmapScalingMode.Linear;
private bool _showZoomLevelInfo = true;
private BitmapSource _source;
private double _zoomFactor = 1d;
private bool _zoomToFit = true;
private double _zoomToFitFactor;
private bool _zoomWithControlKey;
public ImagePanel()
{
private Visibility _backgroundVisibility = Visibility.Visible;
private ContextObject _contextObject;
private Point? _dragInitPos;
private Uri _imageSource;
private bool _isZoomFactorFirstSet = true;
private DateTime _lastZoomTime = DateTime.MinValue;
private double _maxZoomFactor = 3d;
private MetaProvider _meta;
private Visibility _metaIconVisibility = Visibility.Visible;
private double _minZoomFactor = 0.1d;
private BitmapScalingMode _renderMode = BitmapScalingMode.Linear;
private bool _showZoomLevelInfo = true;
private BitmapSource _source;
private double _zoomFactor = 1d;
InitializeComponent();
private bool _zoomToFit = true;
private double _zoomToFitFactor;
private bool _zoomWithControlKey;
Resources.MergedDictionaries.Clear();
public ImagePanel()
buttonMeta.Click += (sender, e) =>
textMeta.Visibility = textMeta.Visibility == Visibility.Collapsed
? Visibility.Visible
: Visibility.Collapsed;
buttonBackgroundColour.Click += OnBackgroundColourOnClick;
SizeChanged += ImagePanel_SizeChanged;
viewPanelImage.DoZoomToFit += (sender, e) => DoZoomToFit();
viewPanelImage.ImageLoaded += (sender, e) => ContextObject.IsBusy = false;
viewPanel.PreviewMouseWheel += ViewPanel_PreviewMouseWheel;
viewPanel.MouseLeftButtonDown += ViewPanel_MouseLeftButtonDown;
viewPanel.MouseMove += ViewPanel_MouseMove;
viewPanel.MouseDoubleClick += ViewPanel_MouseDoubleClick;
viewPanel.ManipulationInertiaStarting += ViewPanel_ManipulationInertiaStarting;
viewPanel.ManipulationStarting += ViewPanel_ManipulationStarting;
viewPanel.ManipulationDelta += ViewPanel_ManipulationDelta;
}
internal ImagePanel(ContextObject context, MetaProvider meta) : this()
{
ContextObject = context;
Meta = meta;
var s = meta.GetSize();
//_minZoomFactor = Math.Min(200d / s.Height, 400d / s.Width);
//_maxZoomFactor = Math.Min(9000d / s.Height, 9000d / s.Width);
ShowMeta();
Theme = ContextObject.Theme;
}
public bool ZoomWithControlKey
{
get => _zoomWithControlKey;
set
{
InitializeComponent();
Resources.MergedDictionaries.Clear();
buttonMeta.Click += (sender, e) =>
textMeta.Visibility = textMeta.Visibility == Visibility.Collapsed
? Visibility.Visible
: Visibility.Collapsed;
buttonBackgroundColour.Click += OnBackgroundColourOnClick;
SizeChanged += ImagePanel_SizeChanged;
viewPanelImage.DoZoomToFit += (sender, e) => DoZoomToFit();
viewPanelImage.ImageLoaded += (sender, e) => ContextObject.IsBusy = false;
viewPanel.PreviewMouseWheel += ViewPanel_PreviewMouseWheel;
viewPanel.MouseLeftButtonDown += ViewPanel_MouseLeftButtonDown;
viewPanel.MouseMove += ViewPanel_MouseMove;
viewPanel.MouseDoubleClick += ViewPanel_MouseDoubleClick;
viewPanel.ManipulationInertiaStarting += ViewPanel_ManipulationInertiaStarting;
viewPanel.ManipulationStarting += ViewPanel_ManipulationStarting;
viewPanel.ManipulationDelta += ViewPanel_ManipulationDelta;
}
internal ImagePanel(ContextObject context, MetaProvider meta) : this()
{
ContextObject = context;
Meta = meta;
var s = meta.GetSize();
//_minZoomFactor = Math.Min(200d / s.Height, 400d / s.Width);
//_maxZoomFactor = Math.Min(9000d / s.Height, 9000d / s.Width);
ShowMeta();
Theme = ContextObject.Theme;
}
public bool ZoomWithControlKey
{
get => _zoomWithControlKey;
set
{
_zoomWithControlKey = value;
OnPropertyChanged();
}
}
public bool ShowZoomLevelInfo
{
get => _showZoomLevelInfo;
set
{
if (value == _showZoomLevelInfo) return;
_showZoomLevelInfo = value;
OnPropertyChanged();
}
}
public Themes Theme
{
get => ContextObject?.Theme ?? Themes.Dark;
set
{
ContextObject.Theme = value;
OnPropertyChanged();
}
}
public BitmapScalingMode RenderMode
{
get => _renderMode;
set
{
_renderMode = value;
OnPropertyChanged();
}
}
public bool ZoomToFit
{
get => _zoomToFit;
set
{
_zoomToFit = value;
OnPropertyChanged();
}
}
public Visibility MetaIconVisibility
{
get => _metaIconVisibility;
set
{
_metaIconVisibility = value;
OnPropertyChanged();
}
}
public Visibility BackgroundVisibility
{
get => _backgroundVisibility;
set
{
_backgroundVisibility = value;
OnPropertyChanged();
}
}
public double MinZoomFactor
{
get => _minZoomFactor;
set
{
_minZoomFactor = value;
OnPropertyChanged();
}
}
public double MaxZoomFactor
{
get => _maxZoomFactor;
set
{
_maxZoomFactor = value;
OnPropertyChanged();
}
}
public double ZoomToFitFactor
{
get => _zoomToFitFactor;
private set
{
_zoomToFitFactor = value;
OnPropertyChanged();
}
}
public double ZoomFactor
{
get => _zoomFactor;
private set
{
_zoomFactor = value;
OnPropertyChanged();
if (_isZoomFactorFirstSet)
{
_isZoomFactorFirstSet = false;
return;
}
if (ShowZoomLevelInfo)
((Storyboard) zoomLevelInfo.FindResource("StoryboardShowZoomLevelInfo")).Begin();
}
}
public Uri ImageUriSource
{
get => _imageSource;
set
{
_imageSource = value;
OnPropertyChanged();
}
}
public BitmapSource Source
{
get => _source;
set
{
_source = value;
OnPropertyChanged();
if (ImageUriSource == null)
viewPanelImage.Source = _source;
}
}
public ContextObject ContextObject
{
get => _contextObject;
set
{
_contextObject = value;
OnPropertyChanged();
}
}
public MetaProvider Meta
{
get => _meta;
set
{
if (Equals(value, _meta)) return;
_meta = value;
OnPropertyChanged();
}
}
public void Dispose()
{
viewPanelImage?.Dispose();
viewPanelImage = null;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnBackgroundColourOnClick(object sender, RoutedEventArgs e)
{
Theme = Theme == Themes.Dark ? Themes.Light : Themes.Dark;
SettingHelper.Set("LastTheme", (int) Theme, "QuickLook.Plugin.ImageViewer");
}
private void ShowMeta()
{
textMeta.Inlines.Clear();
Meta.GetExif().Values.ForEach(m =>
{
if (string.IsNullOrWhiteSpace(m.Item1) || string.IsNullOrWhiteSpace(m.Item2))
return;
textMeta.Inlines.Add(new Run(m.Item1) {FontWeight = FontWeights.SemiBold});
textMeta.Inlines.Add(": ");
textMeta.Inlines.Add(m.Item2);
textMeta.Inlines.Add("\r\n");
});
textMeta.Inlines.Remove(textMeta.Inlines.LastInline);
if (!textMeta.Inlines.Any())
MetaIconVisibility = Visibility.Collapsed;
}
public event EventHandler<int> ImageScrolled;
public event EventHandler ZoomChanged;
private void ImagePanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateZoomToFitFactor();
if (ZoomToFit)
DoZoomToFit();
}
private void ViewPanel_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
{
e.TranslationBehavior = new InertiaTranslationBehavior
{
InitialVelocity = e.InitialVelocities.LinearVelocity,
DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0)
};
}
private void ViewPanel_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.ManipulationContainer = viewPanel;
e.Mode = ManipulationModes.Scale | ManipulationModes.Translate;
}
private void ViewPanel_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
var delta = e.DeltaManipulation;
var newZoom = ZoomFactor + ZoomFactor * (delta.Scale.X - 1);
Zoom(newZoom);
viewPanel.ScrollToHorizontalOffset(viewPanel.HorizontalOffset - delta.Translation.X);
viewPanel.ScrollToVerticalOffset(viewPanel.VerticalOffset - delta.Translation.Y);
e.Handled = true;
}
private void ViewPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.MouseDevice.Capture(viewPanel);
_dragInitPos = e.GetPosition(viewPanel);
var temp = _dragInitPos.Value; // Point is a type value
temp.Offset(viewPanel.HorizontalOffset, viewPanel.VerticalOffset);
_dragInitPos = temp;
}
private void ViewPanel_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DoZoomToFit();
}
private void ViewPanel_MouseMove(object sender, MouseEventArgs e)
{
if (!_dragInitPos.HasValue)
return;
if (e.LeftButton == MouseButtonState.Released)
{
e.MouseDevice.Capture(null);
_dragInitPos = null;
return;
}
e.Handled = true;
var delta = _dragInitPos.Value - e.GetPosition(viewPanel);
viewPanel.ScrollToHorizontalOffset(delta.X);
viewPanel.ScrollToVerticalOffset(delta.Y);
}
private void ViewPanel_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
// normal scroll when Control is not pressed, useful for PdfViewer
if (ZoomWithControlKey && (Keyboard.Modifiers & ModifierKeys.Control) == 0)
{
viewPanel.ScrollToVerticalOffset(viewPanel.VerticalOffset - e.Delta);
ImageScrolled?.Invoke(this, e.Delta);
return;
}
// otherwise, perform normal zooming
var newZoom = ZoomFactor + ZoomFactor * e.Delta / 120 * 0.1;
Zoom(newZoom);
}
public Size GetScrollSize()
{
return new Size(viewPanel.ScrollableWidth, viewPanel.ScrollableHeight);
}
public Point GetScrollPosition()
{
return new Point(viewPanel.HorizontalOffset, viewPanel.VerticalOffset);
}
public void SetScrollPosition(Point point)
{
viewPanel.ScrollToHorizontalOffset(point.X);
viewPanel.ScrollToVerticalOffset(point.Y);
}
public void DoZoomToFit()
{
UpdateZoomToFitFactor();
Zoom(ZoomToFitFactor, false, true);
}
private void UpdateZoomToFitFactor()
{
if (viewPanelImage?.Source == null)
{
ZoomToFitFactor = 1d;
return;
}
var factor = Math.Min(viewPanel.ActualWidth / viewPanelImage.Source.Width,
viewPanel.ActualHeight / viewPanelImage.Source.Height);
ZoomToFitFactor = factor;
}
public void ResetZoom()
{
ZoomToFitFactor = 1;
Zoom(1d, true, ZoomToFit);
}
public void Zoom(double factor, bool suppressEvent = false, bool isToFit = false)
{
if (viewPanelImage?.Source == null)
return;
// pause when fit width
if (ZoomFactor < ZoomToFitFactor && factor > ZoomToFitFactor
|| ZoomFactor > ZoomToFitFactor && factor < ZoomToFitFactor)
{
factor = ZoomToFitFactor;
ZoomToFit = true;
}
// pause when 100%
else if (ZoomFactor < 1 && factor > 1 || ZoomFactor > 1 && factor < 1)
{
factor = 1;
ZoomToFit = false;
}
else
{
if (!isToFit)
ZoomToFit = false;
}
factor = Math.Max(factor, MinZoomFactor);
factor = Math.Min(factor, MaxZoomFactor);
ZoomFactor = factor;
var position = ZoomToFit
? new Point(viewPanelImage.Source.Width / 2, viewPanelImage.Source.Height / 2)
: Mouse.GetPosition(viewPanelImage);
viewPanelImage.LayoutTransform = new ScaleTransform(factor, factor);
viewPanel.InvalidateMeasure();
// critical for calculating offset
viewPanel.ScrollToHorizontalOffset(0);
viewPanel.ScrollToVerticalOffset(0);
UpdateLayout();
var offset = viewPanelImage.TranslatePoint(position, viewPanel) - Mouse.GetPosition(viewPanel);
viewPanel.ScrollToHorizontalOffset(offset.X);
viewPanel.ScrollToVerticalOffset(offset.Y);
UpdateLayout();
if (!suppressEvent)
FireZoomChangedEvent();
}
private void FireZoomChangedEvent()
{
_lastZoomTime = DateTime.Now;
Task.Delay(500).ContinueWith(t =>
{
if (DateTime.Now - _lastZoomTime < TimeSpan.FromSeconds(0.5))
return;
Debug.WriteLine($"FireZoomChangedEvent fired: {Thread.CurrentThread.ManagedThreadId}");
Dispatcher.BeginInvoke(new Action(() => ZoomChanged?.Invoke(this, new EventArgs())),
DispatcherPriority.Background);
});
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void ScrollToTop()
{
viewPanel.ScrollToTop();
}
public void ScrollToBottom()
{
viewPanel.ScrollToBottom();
_zoomWithControlKey = value;
OnPropertyChanged();
}
}
}
public bool ShowZoomLevelInfo
{
get => _showZoomLevelInfo;
set
{
if (value == _showZoomLevelInfo) return;
_showZoomLevelInfo = value;
OnPropertyChanged();
}
}
public Themes Theme
{
get => ContextObject?.Theme ?? Themes.Dark;
set
{
ContextObject.Theme = value;
OnPropertyChanged();
}
}
public BitmapScalingMode RenderMode
{
get => _renderMode;
set
{
_renderMode = value;
OnPropertyChanged();
}
}
public bool ZoomToFit
{
get => _zoomToFit;
set
{
_zoomToFit = value;
OnPropertyChanged();
}
}
public Visibility MetaIconVisibility
{
get => _metaIconVisibility;
set
{
_metaIconVisibility = value;
OnPropertyChanged();
}
}
public Visibility BackgroundVisibility
{
get => _backgroundVisibility;
set
{
_backgroundVisibility = value;
OnPropertyChanged();
}
}
public double MinZoomFactor
{
get => _minZoomFactor;
set
{
_minZoomFactor = value;
OnPropertyChanged();
}
}
public double MaxZoomFactor
{
get => _maxZoomFactor;
set
{
_maxZoomFactor = value;
OnPropertyChanged();
}
}
public double ZoomToFitFactor
{
get => _zoomToFitFactor;
private set
{
_zoomToFitFactor = value;
OnPropertyChanged();
}
}
public double ZoomFactor
{
get => _zoomFactor;
private set
{
_zoomFactor = value;
OnPropertyChanged();
if (_isZoomFactorFirstSet)
{
_isZoomFactorFirstSet = false;
return;
}
if (ShowZoomLevelInfo)
((Storyboard)zoomLevelInfo.FindResource("StoryboardShowZoomLevelInfo")).Begin();
}
}
public Uri ImageUriSource
{
get => _imageSource;
set
{
_imageSource = value;
OnPropertyChanged();
}
}
public BitmapSource Source
{
get => _source;
set
{
_source = value;
OnPropertyChanged();
if (ImageUriSource == null)
viewPanelImage.Source = _source;
}
}
public ContextObject ContextObject
{
get => _contextObject;
set
{
_contextObject = value;
OnPropertyChanged();
}
}
public MetaProvider Meta
{
get => _meta;
set
{
if (Equals(value, _meta)) return;
_meta = value;
OnPropertyChanged();
}
}
public void Dispose()
{
viewPanelImage?.Dispose();
viewPanelImage = null;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnBackgroundColourOnClick(object sender, RoutedEventArgs e)
{
Theme = Theme == Themes.Dark ? Themes.Light : Themes.Dark;
SettingHelper.Set("LastTheme", (int)Theme, "QuickLook.Plugin.ImageViewer");
}
private void ShowMeta()
{
textMeta.Inlines.Clear();
Meta.GetExif().Values.ForEach(m =>
{
if (string.IsNullOrWhiteSpace(m.Item1) || string.IsNullOrWhiteSpace(m.Item2))
return;
textMeta.Inlines.Add(new Run(m.Item1) { FontWeight = FontWeights.SemiBold });
textMeta.Inlines.Add(": ");
textMeta.Inlines.Add(m.Item2);
textMeta.Inlines.Add("\r\n");
});
textMeta.Inlines.Remove(textMeta.Inlines.LastInline);
if (!textMeta.Inlines.Any())
MetaIconVisibility = Visibility.Collapsed;
}
public event EventHandler<int> ImageScrolled;
public event EventHandler ZoomChanged;
private void ImagePanel_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateZoomToFitFactor();
if (ZoomToFit)
DoZoomToFit();
}
private void ViewPanel_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
{
e.TranslationBehavior = new InertiaTranslationBehavior
{
InitialVelocity = e.InitialVelocities.LinearVelocity,
DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0)
};
}
private void ViewPanel_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.ManipulationContainer = viewPanel;
e.Mode = ManipulationModes.Scale | ManipulationModes.Translate;
}
private void ViewPanel_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
var delta = e.DeltaManipulation;
var newZoom = ZoomFactor + ZoomFactor * (delta.Scale.X - 1);
Zoom(newZoom);
viewPanel.ScrollToHorizontalOffset(viewPanel.HorizontalOffset - delta.Translation.X);
viewPanel.ScrollToVerticalOffset(viewPanel.VerticalOffset - delta.Translation.Y);
e.Handled = true;
}
private void ViewPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.MouseDevice.Capture(viewPanel);
_dragInitPos = e.GetPosition(viewPanel);
var temp = _dragInitPos.Value; // Point is a type value
temp.Offset(viewPanel.HorizontalOffset, viewPanel.VerticalOffset);
_dragInitPos = temp;
}
private void ViewPanel_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DoZoomToFit();
}
private void ViewPanel_MouseMove(object sender, MouseEventArgs e)
{
if (!_dragInitPos.HasValue)
return;
if (e.LeftButton == MouseButtonState.Released)
{
e.MouseDevice.Capture(null);
_dragInitPos = null;
return;
}
e.Handled = true;
var delta = _dragInitPos.Value - e.GetPosition(viewPanel);
viewPanel.ScrollToHorizontalOffset(delta.X);
viewPanel.ScrollToVerticalOffset(delta.Y);
}
private void ViewPanel_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
// normal scroll when Control is not pressed, useful for PdfViewer
if (ZoomWithControlKey && (Keyboard.Modifiers & ModifierKeys.Control) == 0)
{
viewPanel.ScrollToVerticalOffset(viewPanel.VerticalOffset - e.Delta);
ImageScrolled?.Invoke(this, e.Delta);
return;
}
// otherwise, perform normal zooming
var newZoom = ZoomFactor + ZoomFactor * e.Delta / 120 * 0.1;
Zoom(newZoom);
}
public Size GetScrollSize()
{
return new Size(viewPanel.ScrollableWidth, viewPanel.ScrollableHeight);
}
public Point GetScrollPosition()
{
return new Point(viewPanel.HorizontalOffset, viewPanel.VerticalOffset);
}
public void SetScrollPosition(Point point)
{
viewPanel.ScrollToHorizontalOffset(point.X);
viewPanel.ScrollToVerticalOffset(point.Y);
}
public void DoZoomToFit()
{
UpdateZoomToFitFactor();
Zoom(ZoomToFitFactor, false, true);
}
private void UpdateZoomToFitFactor()
{
if (viewPanelImage?.Source == null)
{
ZoomToFitFactor = 1d;
return;
}
var factor = Math.Min(viewPanel.ActualWidth / viewPanelImage.Source.Width,
viewPanel.ActualHeight / viewPanelImage.Source.Height);
ZoomToFitFactor = factor;
}
public void ResetZoom()
{
ZoomToFitFactor = 1;
Zoom(1d, true, ZoomToFit);
}
public void Zoom(double factor, bool suppressEvent = false, bool isToFit = false)
{
if (viewPanelImage?.Source == null)
return;
// pause when fit width
if (ZoomFactor < ZoomToFitFactor && factor > ZoomToFitFactor
|| ZoomFactor > ZoomToFitFactor && factor < ZoomToFitFactor)
{
factor = ZoomToFitFactor;
ZoomToFit = true;
}
// pause when 100%
else if (ZoomFactor < 1 && factor > 1 || ZoomFactor > 1 && factor < 1)
{
factor = 1;
ZoomToFit = false;
}
else
{
if (!isToFit)
ZoomToFit = false;
}
factor = Math.Max(factor, MinZoomFactor);
factor = Math.Min(factor, MaxZoomFactor);
ZoomFactor = factor;
var position = ZoomToFit
? new Point(viewPanelImage.Source.Width / 2, viewPanelImage.Source.Height / 2)
: Mouse.GetPosition(viewPanelImage);
viewPanelImage.LayoutTransform = new ScaleTransform(factor, factor);
viewPanel.InvalidateMeasure();
// critical for calculating offset
viewPanel.ScrollToHorizontalOffset(0);
viewPanel.ScrollToVerticalOffset(0);
UpdateLayout();
var offset = viewPanelImage.TranslatePoint(position, viewPanel) - Mouse.GetPosition(viewPanel);
viewPanel.ScrollToHorizontalOffset(offset.X);
viewPanel.ScrollToVerticalOffset(offset.Y);
UpdateLayout();
if (!suppressEvent)
FireZoomChangedEvent();
}
private void FireZoomChangedEvent()
{
_lastZoomTime = DateTime.Now;
Task.Delay(500).ContinueWith(t =>
{
if (DateTime.Now - _lastZoomTime < TimeSpan.FromSeconds(0.5))
return;
Debug.WriteLine($"FireZoomChangedEvent fired: {Thread.CurrentThread.ManagedThreadId}");
Dispatcher.BeginInvoke(new Action(() => ZoomChanged?.Invoke(this, new EventArgs())),
DispatcherPriority.Background);
});
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void ScrollToTop()
{
viewPanel.ScrollToTop();
}
public void ScrollToBottom()
{
viewPanel.ScrollToBottom();
}
}

View File

@@ -15,6 +15,7 @@
// 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 ImageMagick;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -22,172 +23,170 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Xml;
using ImageMagick;
namespace QuickLook.Plugin.ImageViewer
namespace QuickLook.Plugin.ImageViewer;
public class MetaProvider
{
public class MetaProvider
private readonly SortedDictionary<string, (string, string)> _cache =
new SortedDictionary<string, (string, string)>(); // [key, [label, value]]
private readonly string _path;
public MetaProvider(string path)
{
private readonly SortedDictionary<string, (string, string)> _cache =
new SortedDictionary<string, (string, string)>(); // [key, [label, value]]
_path = path;
private readonly string _path;
public MetaProvider(string path)
{
_path = path;
GetExif();
}
public SortedDictionary<string, (string, string)> GetExif()
{
if (_cache.Count != 0)
return _cache;
var exif = NativeMethods.GetExif(_path);
if (string.IsNullOrEmpty(exif))
return _cache;
var xml = new XmlDocument();
xml.LoadXml(exif);
var iter = xml.SelectNodes("/Exif/child::node()")?.GetEnumerator();
while (iter != null && iter.MoveNext())
{
if (!(iter.Current is XmlNode node))
continue;
var key = node.Name;
var label = node.Attributes?["Label"]?.InnerText;
var value = node.InnerText;
_cache.Add(key, (label, value));
}
GetExif();
}
public SortedDictionary<string, (string, string)> GetExif()
{
if (_cache.Count != 0)
return _cache;
}
public byte[] GetThumbnail()
var exif = NativeMethods.GetExif(_path);
if (string.IsNullOrEmpty(exif))
return _cache;
var xml = new XmlDocument();
xml.LoadXml(exif);
var iter = xml.SelectNodes("/Exif/child::node()")?.GetEnumerator();
while (iter != null && iter.MoveNext())
{
return NativeMethods.GetThumbnail(_path) ?? new byte[0];
if (iter.Current is not XmlNode node)
continue;
var key = node.Name;
var label = node.Attributes?["Label"]?.InnerText;
var value = node.InnerText;
_cache.Add(key, (label, value));
}
public Size GetSize()
{
_cache.TryGetValue("_.Size.Width", out var w_);
_cache.TryGetValue("_.Size.Height", out var h_);
if (int.TryParse(w_.Item2, out var w) && int.TryParse(h_.Item2, out var h))
return new Size(w, h);
// fallback
using (var mi = new MagickImage())
{
mi.Ping(_path);
w = (int)mi.Width;
h = (int)mi.Height;
}
return w + h == 0 ? new Size(800, 600) : new Size(w, h);
}
public Orientation GetOrientation()
{
return (Orientation)NativeMethods.GetOrientation(_path);
}
return _cache;
}
internal static class NativeMethods
public byte[] GetThumbnail()
{
private static readonly bool Is64 = Environment.Is64BitProcess;
public static string GetExif(string file)
{
try
{
var len = Is64 ? GetExif_64(file, null) : GetExif_32(file, null);
if (len <= 0)
return string.Empty;
var sb = new StringBuilder(len + 1);
var _ = Is64 ? GetExif_64(file, sb) : GetExif_32(file, sb);
return sb.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e);
return string.Empty;
}
}
public static byte[] GetThumbnail(string file)
{
try
{
var len = Is64 ? GetThumbnail_64(file, null) : GetThumbnail_32(file, null);
if (len <= 0)
return null;
var buffer = new byte[len];
var _ = Is64 ? GetThumbnail_64(file, buffer) : GetThumbnail_32(file, buffer);
return buffer;
}
catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}
public static int GetOrientation(string file)
{
try
{
return Is64 ? GetOrientation_64(file) : GetOrientation_32(file);
}
catch (Exception e)
{
Debug.WriteLine(e);
return 0;
}
}
[DllImport("exiv2-ql-32.dll", EntryPoint = "GetExif", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetExif_32([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder sb);
[DllImport("exiv2-ql-32.dll", EntryPoint = "GetThumbnail", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetThumbnail_32([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPArray)] byte[] buffer);
[DllImport("exiv2-ql-32.dll", EntryPoint = "GetOrientation", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetOrientation_32([MarshalAs(UnmanagedType.LPWStr)] string file);
[DllImport("exiv2-ql-64.dll", EntryPoint = "GetExif", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetExif_64([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder sb);
[DllImport("exiv2-ql-64.dll", EntryPoint = "GetThumbnail", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetThumbnail_64([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPArray)] byte[] buffer);
[DllImport("exiv2-ql-64.dll", EntryPoint = "GetOrientation", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetOrientation_64([MarshalAs(UnmanagedType.LPWStr)] string file);
return NativeMethods.GetThumbnail(_path) ?? [];
}
public enum Orientation
public Size GetSize()
{
Undefined = 0,
TopLeft = 1,
TopRight = 2,
BottomRight = 3,
BottomLeft = 4,
LeftTop = 5,
RightTop = 6,
RightBottom = 7,
LeftBottom = 8
_cache.TryGetValue("_.Size.Width", out var w_);
_cache.TryGetValue("_.Size.Height", out var h_);
if (int.TryParse(w_.Item2, out var w) && int.TryParse(h_.Item2, out var h))
return new Size(w, h);
// fallback
using (var mi = new MagickImage())
{
mi.Ping(_path);
w = (int)mi.Width;
h = (int)mi.Height;
}
return w + h == 0 ? new Size(800, 600) : new Size(w, h);
}
public Orientation GetOrientation()
{
return (Orientation)NativeMethods.GetOrientation(_path);
}
}
internal static class NativeMethods
{
private static readonly bool Is64 = Environment.Is64BitProcess;
public static string GetExif(string file)
{
try
{
var len = Is64 ? GetExif_64(file, null) : GetExif_32(file, null);
if (len <= 0)
return string.Empty;
var sb = new StringBuilder(len + 1);
var _ = Is64 ? GetExif_64(file, sb) : GetExif_32(file, sb);
return sb.ToString();
}
catch (Exception e)
{
Debug.WriteLine(e);
return string.Empty;
}
}
public static byte[] GetThumbnail(string file)
{
try
{
var len = Is64 ? GetThumbnail_64(file, null) : GetThumbnail_32(file, null);
if (len <= 0)
return null;
var buffer = new byte[len];
var _ = Is64 ? GetThumbnail_64(file, buffer) : GetThumbnail_32(file, buffer);
return buffer;
}
catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}
public static int GetOrientation(string file)
{
try
{
return Is64 ? GetOrientation_64(file) : GetOrientation_32(file);
}
catch (Exception e)
{
Debug.WriteLine(e);
return 0;
}
}
[DllImport("exiv2-ql-32.dll", EntryPoint = "GetExif", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetExif_32([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder sb);
[DllImport("exiv2-ql-32.dll", EntryPoint = "GetThumbnail", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetThumbnail_32([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPArray)] byte[] buffer);
[DllImport("exiv2-ql-32.dll", EntryPoint = "GetOrientation", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetOrientation_32([MarshalAs(UnmanagedType.LPWStr)] string file);
[DllImport("exiv2-ql-64.dll", EntryPoint = "GetExif", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetExif_64([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder sb);
[DllImport("exiv2-ql-64.dll", EntryPoint = "GetThumbnail", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetThumbnail_64([MarshalAs(UnmanagedType.LPWStr)] string file,
[MarshalAs(UnmanagedType.LPArray)] byte[] buffer);
[DllImport("exiv2-ql-64.dll", EntryPoint = "GetOrientation", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetOrientation_64([MarshalAs(UnmanagedType.LPWStr)] string file);
}
public enum Orientation
{
Undefined = 0,
TopLeft = 1,
TopRight = 2,
BottomRight = 3,
BottomLeft = 4,
LeftTop = 5,
RightTop = 6,
RightBottom = 7,
LeftBottom = 8
}

View File

@@ -1,17 +1,17 @@
// 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/>.
@@ -24,115 +24,114 @@ using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
namespace QuickLook.Plugin.ImageViewer
namespace QuickLook.Plugin.ImageViewer;
public class Plugin : IViewer
{
public class Plugin : IViewer
private static readonly HashSet<string> WellKnownImageExtensions = 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", ".jp2", ".jpeg", ".jpg", ".jxl",
".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", ".xcf"
]);
private ImagePanel _ip;
private MetaProvider _meta;
public int Priority => 0;
public void Init()
{
private static readonly HashSet<string> WellKnownImageExtensions = new HashSet<string>(new[]
var useColorProfile = SettingHelper.Get("UseColorProfile", false, "QuickLook.Plugin.ImageViewer");
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(
useColorProfile ? [".apng"] : [".apng", ".png"],
typeof(APngProvider)));
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>([".gif"],
typeof(GifProvider)));
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(
useColorProfile ? [] : [".bmp", ".jpg", ".jpeg", ".jfif", ".tif", ".tiff"],
typeof(NativeProvider)));
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(["*"],
typeof(ImageMagickProvider)));
}
private bool IsWellKnownImageExtension(string path)
{
return WellKnownImageExtensions.Contains(Path.GetExtension(path.ToLower()));
}
private bool IsImageMagickSupported(string path)
{
try
{
".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", ".jp2", ".jpeg", ".jpg", ".jxl",
".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", ".xcf"
});
private ImagePanel _ip;
private MetaProvider _meta;
public int Priority => 0;
public void Init()
{
var useColorProfile = SettingHelper.Get("UseColorProfile", false, "QuickLook.Plugin.ImageViewer");
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(
useColorProfile ? new[] { ".apng" } : 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>(
useColorProfile ? new string[0] : new[] { ".bmp", ".jpg", ".jpeg", ".jfif", ".tif", ".tiff" },
typeof(NativeProvider)));
AnimatedImage.AnimatedImage.Providers.Add(
new KeyValuePair<string[], Type>(new[] {"*"},
typeof(ImageMagickProvider)));
return new MagickImageInfo(path).Format != MagickFormat.Unknown;
}
private bool IsWellKnownImageExtension(string path)
catch
{
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)
{
// Disabled due mishandling text file types e.g., "*.config".
// 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;
return false;
}
}
public bool CanHandle(string path)
{
// Disabled due mishandling text file types e.g., "*.config".
// 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;
}
}