mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-11 09:49:07 +00:00
[WIP] apng provider
This commit is contained in:
@@ -33,17 +33,16 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
{
|
||||
private readonly List<FrameInfo> _frames;
|
||||
private readonly List<BitmapSource> _renderedFrames;
|
||||
private ImageMagickProvider _imageMagickProvider;
|
||||
private NETImageProvider _imageMagickProvider;
|
||||
private int _lastEffecitvePreviousPreviousFrameIndex;
|
||||
|
||||
public APNGAnimationProvider(string path, NConvert meta, Dispatcher uiDispatcher) : base(path, meta,
|
||||
uiDispatcher)
|
||||
public APNGAnimationProvider(string path) : base(path)
|
||||
{
|
||||
var decoder = new APNGBitmap(path);
|
||||
|
||||
if (decoder.IsSimplePNG)
|
||||
{
|
||||
_imageMagickProvider = new ImageMagickProvider(path, meta, uiDispatcher);
|
||||
_imageMagickProvider = new NETImageProvider(path);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -66,6 +65,11 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetThumbnail(Size size, Size fullSize)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetRenderedFrame(int index)
|
||||
{
|
||||
if (_imageMagickProvider != null)
|
||||
|
@@ -15,8 +15,15 @@
|
||||
// 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.Plugin;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Threading;
|
||||
@@ -25,9 +32,15 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
{
|
||||
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 event EventHandler ImageLoaded;
|
||||
public event EventHandler DoZoomToFit;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_disposing = true;
|
||||
@@ -39,23 +52,12 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
_animation = null;
|
||||
}
|
||||
|
||||
private static AnimationProvider LoadFullImageCore(Uri path, NConvert meta, Dispatcher uiDispatcher)
|
||||
private static AnimationProvider LoadFullImageCore(Uri path)
|
||||
{
|
||||
byte[] sign;
|
||||
using (var reader =
|
||||
new BinaryReader(new FileStream(path.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
|
||||
{
|
||||
sign = reader.BaseStream.Length < 4 ? new byte[] {0, 0, 0, 0} : reader.ReadBytes(4);
|
||||
}
|
||||
var ext = Path.GetExtension(path.LocalPath).ToLower();
|
||||
var type = Providers.First(p => p.Key.Contains(ext) || p.Key.Contains("*")).Value;
|
||||
|
||||
AnimationProvider provider;
|
||||
|
||||
if (sign[0] == 'G' && sign[1] == 'I' && sign[2] == 'F' && sign[3] == '8')
|
||||
provider = new GifAnimationProvider(path.LocalPath, meta, uiDispatcher);
|
||||
else if (sign[0] == 0x89 && sign[1] == 'P' && sign[2] == 'N' && sign[3] == 'G')
|
||||
provider = new APNGAnimationProvider(path.LocalPath, meta, uiDispatcher);
|
||||
else
|
||||
provider = new ImageMagickProvider(path.LocalPath, meta, uiDispatcher);
|
||||
var provider = type.CreateInstance<AnimationProvider>(path.LocalPath);
|
||||
|
||||
return provider;
|
||||
}
|
||||
@@ -64,7 +66,7 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
|
||||
public static readonly DependencyProperty AnimationFrameIndexProperty =
|
||||
DependencyProperty.Register("AnimationFrameIndex", typeof(int), typeof(AnimatedImage),
|
||||
new UIPropertyMetadata(-1, AnimationFrameIndexChanged));
|
||||
new UIPropertyMetadata(-2, AnimationFrameIndexChanged));
|
||||
|
||||
public static readonly DependencyProperty AnimationUriProperty =
|
||||
DependencyProperty.Register("AnimationUri", typeof(Uri), typeof(AnimatedImage),
|
||||
@@ -73,24 +75,33 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
public static readonly DependencyProperty MetaProperty =
|
||||
DependencyProperty.Register("Meta", typeof(NConvert), typeof(AnimatedImage));
|
||||
|
||||
public static readonly DependencyProperty ContextObjectProperty =
|
||||
DependencyProperty.Register("ContextObject", typeof(ContextObject), typeof(AnimatedImage));
|
||||
|
||||
public int AnimationFrameIndex
|
||||
{
|
||||
get => (int) GetValue(AnimationFrameIndexProperty);
|
||||
get => (int)GetValue(AnimationFrameIndexProperty);
|
||||
set => SetValue(AnimationFrameIndexProperty, value);
|
||||
}
|
||||
|
||||
public Uri AnimationUri
|
||||
{
|
||||
get => (Uri) GetValue(AnimationUriProperty);
|
||||
get => (Uri)GetValue(AnimationUriProperty);
|
||||
set => SetValue(AnimationUriProperty, value);
|
||||
}
|
||||
|
||||
public NConvert Meta
|
||||
{
|
||||
private get => (NConvert) GetValue(MetaProperty);
|
||||
private get => (NConvert)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))
|
||||
@@ -99,10 +110,10 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
//var thumbnail = instance.Meta?.GetThumbnail(true);
|
||||
//instance.Source = thumbnail;
|
||||
|
||||
instance._animation = LoadFullImageCore((Uri) ev.NewValue, instance.Meta, instance.Dispatcher);
|
||||
instance._animation = LoadFullImageCore((Uri)ev.NewValue);
|
||||
|
||||
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation.Animator);
|
||||
instance.AnimationFrameIndex = 0;
|
||||
instance.AnimationFrameIndex = -1;
|
||||
}
|
||||
|
||||
private static void AnimationFrameIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
|
||||
@@ -113,22 +124,32 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
if (instance._disposing)
|
||||
return;
|
||||
|
||||
var task = instance._animation.GetRenderedFrame((int) ev.NewValue);
|
||||
|
||||
if (instance.Source == null && (int) ev.NewValue == 0) // this is the first image. Run it synchronously.
|
||||
if ((int)ev.NewValue == -1) // get thumbnail
|
||||
{
|
||||
var task = instance._animation.GetThumbnail(instance.ContextObject.PreferredSize, instance.Meta.GetSize());
|
||||
|
||||
if (task != null)
|
||||
{
|
||||
task.ContinueWith(_ => instance.Dispatcher.Invoke(() =>
|
||||
{
|
||||
instance.Source = _.Result;
|
||||
instance.DoZoomToFit?.Invoke(instance, new EventArgs());
|
||||
instance.ImageLoaded?.Invoke(instance, new EventArgs());
|
||||
}));
|
||||
task.Start();
|
||||
}
|
||||
}
|
||||
else // begin to loop in the animator
|
||||
{
|
||||
var task = instance._animation.GetRenderedFrame((int)ev.NewValue);
|
||||
|
||||
task.ContinueWith(_ => instance.Dispatcher.Invoke(() =>
|
||||
{
|
||||
instance.Source = _.Result;
|
||||
}));
|
||||
task.Start();
|
||||
task.Wait(5000);
|
||||
}
|
||||
|
||||
if (task.IsCompleted)
|
||||
{
|
||||
instance.Source = task.Result;
|
||||
return;
|
||||
}
|
||||
|
||||
task.ContinueWith(t => { instance.Dispatcher.Invoke(() => instance.Source = t.Result); });
|
||||
task.Start();
|
||||
}
|
||||
|
||||
#endregion DependencyProperty
|
||||
|
@@ -17,6 +17,7 @@
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
@@ -25,23 +26,19 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
{
|
||||
internal abstract class AnimationProvider : IDisposable
|
||||
{
|
||||
protected AnimationProvider(string path, NConvert meta, Dispatcher uiDispatcher)
|
||||
protected AnimationProvider(string path)
|
||||
{
|
||||
Path = path;
|
||||
Meta = meta;
|
||||
Dispatcher = uiDispatcher;
|
||||
}
|
||||
|
||||
public Dispatcher Dispatcher { get; }
|
||||
|
||||
public string Path { get; }
|
||||
|
||||
public NConvert Meta { get; }
|
||||
|
||||
public Int32AnimationUsingKeyFrames Animator { get; protected set; }
|
||||
|
||||
public abstract void Dispose();
|
||||
|
||||
public abstract Task<BitmapSource> GetThumbnail(Size size, Size fullSize);
|
||||
|
||||
public abstract Task<BitmapSource> GetRenderedFrame(int index);
|
||||
}
|
||||
}
|
@@ -18,6 +18,7 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
@@ -27,50 +28,58 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
{
|
||||
internal class GifAnimationProvider : AnimationProvider
|
||||
{
|
||||
private Bitmap _frame;
|
||||
private BitmapSource _frameSource;
|
||||
private Bitmap _fileHandle;
|
||||
private BitmapSource _frame;
|
||||
private bool _isPlaying;
|
||||
|
||||
public GifAnimationProvider(string path, NConvert meta, Dispatcher uiDispatcher) : base(path, meta,
|
||||
uiDispatcher)
|
||||
public GifAnimationProvider(string path) : base(path)
|
||||
{
|
||||
_frame = (Bitmap) Image.FromFile(path);
|
||||
_frameSource = _frame.ToBitmapSource();
|
||||
|
||||
Animator = new Int32AnimationUsingKeyFrames {RepeatBehavior = RepeatBehavior.Forever};
|
||||
_fileHandle = (Bitmap)Image.FromFile(path);
|
||||
|
||||
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(50))));
|
||||
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(2, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(100))));
|
||||
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()
|
||||
{
|
||||
if (_frame == null)
|
||||
if (_fileHandle == null)
|
||||
return;
|
||||
|
||||
ImageAnimator.StopAnimate(_frame, OnFrameChanged);
|
||||
_frame.Dispose();
|
||||
ImageAnimator.StopAnimate(_fileHandle, OnFrameChanged);
|
||||
_fileHandle.Dispose();
|
||||
|
||||
_fileHandle = null;
|
||||
_frame = null;
|
||||
_frameSource = null;
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetThumbnail(System.Windows.Size size, System.Windows.Size fullSize)
|
||||
{
|
||||
return new Task<BitmapSource>(() =>
|
||||
{
|
||||
return _fileHandle.ToBitmapSource();
|
||||
});
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetRenderedFrame(int index)
|
||||
{
|
||||
if (!_isPlaying)
|
||||
return new Task<BitmapSource>(() =>
|
||||
{
|
||||
_isPlaying = true;
|
||||
ImageAnimator.Animate(_frame, OnFrameChanged);
|
||||
}
|
||||
if (!_isPlaying)
|
||||
{
|
||||
_isPlaying = true;
|
||||
ImageAnimator.Animate(_fileHandle, OnFrameChanged);
|
||||
}
|
||||
|
||||
return new Task<BitmapSource>(() => _frameSource);
|
||||
return _frame;
|
||||
});
|
||||
}
|
||||
|
||||
private void OnFrameChanged(object sender, EventArgs e)
|
||||
{
|
||||
ImageAnimator.UpdateFrames();
|
||||
_frameSource = _frame.ToBitmapSource();
|
||||
_frame = _fileHandle.ToBitmapSource();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -17,36 +17,55 @@
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Animation;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||
{
|
||||
internal class ImageMagickProvider : AnimationProvider
|
||||
internal class NETImageProvider : AnimationProvider
|
||||
{
|
||||
public ImageMagickProvider(string path, NConvert meta, Dispatcher uiDispatcher) : base(path, meta, uiDispatcher)
|
||||
public NETImageProvider(string path) : base(path)
|
||||
{
|
||||
Animator = new Int32AnimationUsingKeyFrames();
|
||||
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0,
|
||||
KeyTime.FromTimeSpan(TimeSpan.Zero))); // thumbnail/full image
|
||||
KeyTime.FromTimeSpan(TimeSpan.Zero)));
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetThumbnail(Size size, Size fullSize)
|
||||
{
|
||||
var factor = Math.Min(size.Width / 2 / fullSize.Width, size.Height / 2 / fullSize.Height);
|
||||
var decode_width = fullSize.Width * factor;
|
||||
|
||||
return new Task<BitmapSource>(() =>
|
||||
{
|
||||
var img = new BitmapImage();
|
||||
img.BeginInit();
|
||||
img.UriSource = new Uri(Path);
|
||||
img.CacheOption = BitmapCacheOption.OnLoad;
|
||||
img.DecodePixelWidth = (int)Math.Floor(decode_width);
|
||||
img.EndInit();
|
||||
|
||||
var scaled = new TransformedBitmap(img, new ScaleTransform(1d / factor, 1d / factor));
|
||||
scaled.Freeze();
|
||||
return scaled;
|
||||
});
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetRenderedFrame(int index)
|
||||
{
|
||||
return new Task<BitmapSource>(() =>
|
||||
{
|
||||
using (var ms = Meta.GetPngStream())
|
||||
{
|
||||
var img = new BitmapImage();
|
||||
img.BeginInit();
|
||||
img.StreamSource = ms;
|
||||
img.CacheOption = BitmapCacheOption.OnLoad;
|
||||
img.EndInit();
|
||||
img.Freeze();
|
||||
var img = new BitmapImage();
|
||||
img.BeginInit();
|
||||
img.UriSource = new Uri(Path);
|
||||
img.CacheOption = BitmapCacheOption.OnLoad;
|
||||
img.EndInit();
|
||||
|
||||
return img;
|
||||
}
|
||||
img.Freeze();
|
||||
return img;
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user