working on new Gif viewer

This commit is contained in:
Paddy Xu
2018-06-13 18:43:33 +03:00
parent d4feb2c867
commit c13839cb19
11 changed files with 429 additions and 139 deletions

View File

@@ -16,13 +16,11 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows.Threading;
using QuickLook.Common.Helpers;
using QuickLook.Plugin.ImageViewer.Exiv2;
@@ -31,15 +29,68 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
{
public class AnimatedImage : Image, IDisposable
{
private AnimationProvider _animation;
private bool _disposed;
public void Dispose()
{
BeginAnimation(AnimationFrameIndexProperty, null);
Source = null;
_animation = null;
_disposed = true;
Task.Delay(500).ContinueWith(t => ProcessHelper.PerformAggressiveGC());
}
private static void LoadFullImage(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
if (!(obj is AnimatedImage instance))
return;
instance._animation = LoadFullImageCore((Uri) ev.NewValue);
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation.Animator);
}
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);
}
AnimationProvider provider = null;
if (sign[0] == 'G' && sign[1] == 'I' && sign[2] == 'F' && sign[3] == '8')
provider = new GIFAnimationProvider(path.LocalPath);
//else if (sign[0] == 0x89 && sign[1] == 'P' && sign[2] == 'N' && sign[3] == 'G')
// provider = new APNGAnimationProvider();
//else
// provider = new ImageMagickProvider();
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, LoadImage));
new UIPropertyMetadata(null, AnimationUriChanged));
public static readonly DependencyProperty MetaProperty =
DependencyProperty.Register("Meta", typeof(Meta), typeof(AnimatedImage));
private ObjectAnimationUsingKeyFrames _animator = new ObjectAnimationUsingKeyFrames();
private bool _disposed;
public int AnimationFrameIndex
{
get => (int) GetValue(AnimationFrameIndexProperty);
set => SetValue(AnimationFrameIndexProperty, value);
}
public Uri AnimationUri
{
@@ -53,90 +104,33 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
set => SetValue(MetaProperty, value);
}
public void Dispose()
private static void AnimationUriChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
BeginAnimation(SourceProperty, null);
Source = null;
_animator.KeyFrames.Clear();
_disposed = true;
}
private static void LoadImage(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
var instance = obj as AnimatedImage;
if (instance == null)
if (!(obj is AnimatedImage instance))
return;
var thumbnail = instance.Meta?.GetThumbnail(true);
instance.Source = thumbnail;
//var thumbnail = instance.Meta?.GetThumbnail(true);
//instance.Source = thumbnail;
if (thumbnail != null)
LoadFullImageAsync(obj, ev);
else
LoadFullImage(obj, ev);
LoadFullImage(obj, ev);
instance.AnimationFrameIndex = 0;
}
private static void LoadFullImage(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
private static void AnimationFrameIndexChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
var instance = obj as AnimatedImage;
if (instance == null)
if (!(obj is AnimatedImage instance))
return;
instance._animator = LoadFullImageCore((Uri) ev.NewValue);
instance.BeginAnimation(SourceProperty, instance._animator);
}
private static void LoadFullImageAsync(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
Task.Run(() =>
new Task(() =>
{
var instance = obj as AnimatedImage;
if (instance == null)
return;
var image = instance._animation.GetRenderedFrame((int) ev.NewValue);
var animator = LoadFullImageCore((Uri) ev.NewValue);
instance.Dispatcher.Invoke(DispatcherPriority.Render,
new Action(() =>
{
if (instance._disposed)
{
ProcessHelper.PerformAggressiveGC();
return;
}
instance._animator = animator;
instance.BeginAnimation(SourceProperty, instance._animator);
Debug.WriteLine($"LoadFullImageAsync {Thread.CurrentThread.ManagedThreadId}");
}));
});
instance.Dispatcher.BeginInvoke(
new Action(() => { instance.Source = image; }), DispatcherPriority.Loaded);
}).Start();
}
private static ObjectAnimationUsingKeyFrames 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);
}
IAnimationProvider provider;
if (sign[0] == 'G' && sign[1] == 'I' && sign[2] == 'F' && sign[3] == '8')
provider = new GIFAnimationProvider();
else if (sign[0] == 0x89 && sign[1] == 'P' && sign[2] == 'N' && sign[3] == 'G')
provider = new APNGAnimationProvider();
else
provider = new ImageMagickProvider();
var animator = new ObjectAnimationUsingKeyFrames();
provider.GetAnimator(animator, path.LocalPath);
animator.Freeze();
return animator;
}
#endregion DependencyProperty
}
}

View File

@@ -15,12 +15,23 @@
// 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.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
{
internal interface IAnimationProvider
internal abstract class AnimationProvider
{
void GetAnimator(ObjectAnimationUsingKeyFrames animator, string path);
public AnimationProvider(string path)
{
Path = path;
}
public string Path { get; }
public Int32Animation Animator { get; protected set; }
public abstract DrawingImage GetRenderedFrame(int index);
}
}

View File

@@ -16,86 +16,91 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using QuickLook.Common.ExtensionMethods;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
{
internal class GIFAnimationProvider : IAnimationProvider
internal class GIFAnimationProvider : AnimationProvider
{
public void GetAnimator(ObjectAnimationUsingKeyFrames animator, string path)
private readonly List<FrameInfo> _decodedFrames;
private readonly int _lastRenderedFrameIndex;
private readonly DrawingGroup renderedFrame;
public GIFAnimationProvider(string path) : base(path)
{
var decoder =
new GifBitmapDecoder(new Uri(path), BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnLoad);
var decoder = new GifBitmapDecoder(new Uri(path), BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnLoad);
var clock = TimeSpan.Zero;
BitmapSource prevFrame = null;
FrameInfo prevInfo = null;
BitmapSource prevprevFrame = null;
foreach (var rawFrame in decoder.Frames)
_decodedFrames = new List<FrameInfo>(decoder.Frames.Count);
decoder.Frames.ForEach(f => _decodedFrames.Add(GetFrameInfo(f)));
renderedFrame = new DrawingGroup();
_lastRenderedFrameIndex = -1;
var delay = _decodedFrames[0].Delay.TotalMilliseconds;
Animator = new Int32Animation(0, decoder.Frames.Count - 1,
new Duration(TimeSpan.FromMilliseconds(delay * (decoder.Frames.Count - 1))))
{
var info = GetFrameInfo(rawFrame);
var frame = MakeFrame(decoder.Frames[0], rawFrame, info, prevFrame, prevInfo, prevprevFrame);
prevprevFrame = prevFrame;
prevFrame = frame;
prevInfo = info;
RepeatBehavior = RepeatBehavior.Forever
};
}
animator.KeyFrames.Add(new DiscreteObjectKeyFrame(frame, clock));
clock += info.Delay;
}
public override DrawingImage GetRenderedFrame(int index)
{
for (var i = _lastRenderedFrameIndex + 1; i < index; i++)
MakeFrame(renderedFrame, _decodedFrames[i], i > 0 ? _decodedFrames[i - 1] : null);
animator.Duration = clock;
animator.RepeatBehavior = RepeatBehavior.Forever;
MakeFrame(
renderedFrame,
_decodedFrames[index],
index > 0 ? _decodedFrames[index - 1] : null);
var di=new DrawingImage(renderedFrame);
di.Freeze();
return di;
}
#region private methods
private static BitmapSource MakeFrame(
BitmapSource fullImage,
BitmapSource rawFrame, FrameInfo frameInfo,
BitmapSource previousFrame, FrameInfo previousFrameInfo,
BitmapSource previouspreviousFrame)
private static void MakeFrame(
DrawingGroup renderedFrame,
FrameInfo currentFrame,
FrameInfo previousFrame)
{
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
if (previousFrameInfo != null && previousFrame != null)
if (previousFrame == null)
renderedFrame.Children.Clear();
else
switch (previousFrame.DisposalMethod)
{
var fullRect = new Rect(0, 0, fullImage.PixelWidth, fullImage.PixelHeight);
switch (previousFrameInfo.DisposalMethod)
{
case FrameDisposalMethod.Unspecified:
case FrameDisposalMethod.Combine:
context.DrawImage(previousFrame, fullRect);
break;
case FrameDisposalMethod.RestorePrevious:
if (previouspreviousFrame != null)
context.DrawImage(previouspreviousFrame, fullRect);
break;
case FrameDisposalMethod.RestoreBackground:
break;
}
case FrameDisposalMethod.Unspecified:
case FrameDisposalMethod.Combine:
break;
case FrameDisposalMethod.RestorePrevious:
renderedFrame.Children.RemoveAt(renderedFrame.Children.Count - 1);
break;
case FrameDisposalMethod.RestoreBackground:
var bg = renderedFrame.Children.First();
renderedFrame.Children.Clear();
renderedFrame.Children.Add(bg);
break;
}
context.DrawImage(rawFrame, frameInfo.Rect);
}
var bitmap = new RenderTargetBitmap(
fullImage.PixelWidth, fullImage.PixelHeight,
Math.Floor(fullImage.DpiX), Math.Floor(fullImage.DpiY),
PixelFormats.Pbgra32);
bitmap.Render(visual);
return bitmap;
renderedFrame.Children.Add(new ImageDrawing(currentFrame.Frame, currentFrame.Rect));
}
private static FrameInfo GetFrameInfo(BitmapFrame frame)
{
var frameInfo = new FrameInfo
{
Frame = frame,
Delay = TimeSpan.FromMilliseconds(100),
DisposalMethod = FrameDisposalMethod.Unspecified,
Width = frame.PixelWidth,
@@ -153,14 +158,15 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
private class FrameInfo
{
public TimeSpan Delay { get; set; }
public BitmapSource Frame { get; set; }
public FrameDisposalMethod DisposalMethod { get; set; }
public TimeSpan Delay { get; set; }
public Rect Rect => new Rect(Left, Top, Width, Height);
public double Width { private get; set; }
public double Height { private get; set; }
public double Left { private get; set; }
public double Top { private get; set; }
public Rect Rect => new Rect(Left, Top, Width, Height);
}
private enum FrameDisposalMethod

View File

@@ -78,10 +78,10 @@
<Link>Properties\GitVersion.cs</Link>
</Compile>
<Compile Include="AnimatedImage\AnimatedImage.cs" />
<Compile Include="AnimatedImage\APNGAnimationProvider.cs" />
<None Include="AnimatedImage\APNGAnimationProvider.cs" />
<Compile Include="AnimatedImage\GIFAnimationProvider.cs" />
<Compile Include="AnimatedImage\IAnimationProvider.cs" />
<Compile Include="AnimatedImage\ImageMagickProvider.cs" />
<Compile Include="AnimatedImage\AnimationProvider.cs" />
<None Include="AnimatedImage\ImageMagickProvider.cs" />
<Compile Include="exiv2\Meta.cs" />
<Compile Include="ImageFileHelper.cs" />
<Compile Include="ImagePanel.xaml.cs">