mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-11 17:59:17 +00:00
done new Gif render
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// Copyright © 2017 Paddy Xu
|
// Copyright © 2018 Paddy Xu
|
||||||
//
|
//
|
||||||
// This file is part of QuickLook program.
|
// This file is part of QuickLook program.
|
||||||
//
|
//
|
||||||
@@ -17,12 +17,9 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using System.Windows.Media;
|
|
||||||
using System.Windows.Threading;
|
using System.Windows.Threading;
|
||||||
using QuickLook.Common.Helpers;
|
|
||||||
using QuickLook.Plugin.ImageViewer.Exiv2;
|
using QuickLook.Plugin.ImageViewer.Exiv2;
|
||||||
|
|
||||||
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||||
@@ -30,17 +27,14 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
|||||||
public class AnimatedImage : Image, IDisposable
|
public class AnimatedImage : Image, IDisposable
|
||||||
{
|
{
|
||||||
private AnimationProvider _animation;
|
private AnimationProvider _animation;
|
||||||
private bool _disposed;
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
BeginAnimation(AnimationFrameIndexProperty, null);
|
BeginAnimation(AnimationFrameIndexProperty, null);
|
||||||
Source = null;
|
Source = null;
|
||||||
|
|
||||||
|
_animation?.Dispose();
|
||||||
_animation = null;
|
_animation = null;
|
||||||
|
|
||||||
_disposed = true;
|
|
||||||
|
|
||||||
Task.Delay(500).ContinueWith(t => ProcessHelper.PerformAggressiveGC());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void LoadFullImage(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
|
private static void LoadFullImage(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
|
||||||
@@ -48,11 +42,11 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
|||||||
if (!(obj is AnimatedImage instance))
|
if (!(obj is AnimatedImage instance))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
instance._animation = LoadFullImageCore((Uri) ev.NewValue);
|
instance._animation = LoadFullImageCore((Uri) ev.NewValue, instance.Dispatcher);
|
||||||
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation.Animator);
|
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation.Animator);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static AnimationProvider LoadFullImageCore(Uri path)
|
private static AnimationProvider LoadFullImageCore(Uri path, Dispatcher uiDispatcher)
|
||||||
{
|
{
|
||||||
byte[] sign;
|
byte[] sign;
|
||||||
using (var reader =
|
using (var reader =
|
||||||
@@ -64,7 +58,7 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
|||||||
AnimationProvider provider = null;
|
AnimationProvider provider = null;
|
||||||
|
|
||||||
if (sign[0] == 'G' && sign[1] == 'I' && sign[2] == 'F' && sign[3] == '8')
|
if (sign[0] == 'G' && sign[1] == 'I' && sign[2] == 'F' && sign[3] == '8')
|
||||||
provider = new GIFAnimationProvider(path.LocalPath);
|
provider = new GifAnimationProvider(path.LocalPath, uiDispatcher);
|
||||||
//else if (sign[0] == 0x89 && sign[1] == 'P' && sign[2] == 'N' && sign[3] == 'G')
|
//else if (sign[0] == 0x89 && sign[1] == 'P' && sign[2] == 'N' && sign[3] == 'G')
|
||||||
// provider = new APNGAnimationProvider();
|
// provider = new APNGAnimationProvider();
|
||||||
//else
|
//else
|
||||||
@@ -122,13 +116,9 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
|||||||
if (!(obj is AnimatedImage instance))
|
if (!(obj is AnimatedImage instance))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
new Task(() =>
|
var image = instance._animation.GetRenderedFrame((int) ev.NewValue);
|
||||||
{
|
//if (!ReferenceEquals(instance.Source, image))
|
||||||
var image = instance._animation.GetRenderedFrame((int) ev.NewValue);
|
instance.Source = image;
|
||||||
|
|
||||||
instance.Dispatcher.BeginInvoke(
|
|
||||||
new Action(() => { instance.Source = image; }), DispatcherPriority.Loaded);
|
|
||||||
}).Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion DependencyProperty
|
#endregion DependencyProperty
|
||||||
|
@@ -15,23 +15,29 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Animation;
|
using System.Windows.Media.Animation;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Threading;
|
||||||
|
|
||||||
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||||
{
|
{
|
||||||
internal abstract class AnimationProvider
|
internal abstract class AnimationProvider : IDisposable
|
||||||
{
|
{
|
||||||
public AnimationProvider(string path)
|
protected AnimationProvider(string path, Dispatcher uiDispatcher)
|
||||||
{
|
{
|
||||||
Path = path;
|
Path = path;
|
||||||
|
Dispatcher = uiDispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Dispatcher Dispatcher { get; }
|
||||||
|
|
||||||
public string Path { get; }
|
public string Path { get; }
|
||||||
|
|
||||||
public Int32Animation Animator { get; protected set; }
|
public Int32Animation Animator { get; protected set; }
|
||||||
|
|
||||||
public abstract DrawingImage GetRenderedFrame(int index);
|
public abstract void Dispose();
|
||||||
|
|
||||||
|
public abstract ImageSource GetRenderedFrame(int index);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
// Copyright © 2017 Paddy Xu
|
// Copyright © 2018 Paddy Xu
|
||||||
//
|
//
|
||||||
// This file is part of QuickLook program.
|
// This file is part of QuickLook program.
|
||||||
//
|
//
|
||||||
@@ -16,187 +16,60 @@
|
|||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Drawing;
|
||||||
using System.Linq;
|
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Media.Animation;
|
using System.Windows.Media.Animation;
|
||||||
using System.Windows.Media.Imaging;
|
using System.Windows.Media.Imaging;
|
||||||
|
using System.Windows.Threading;
|
||||||
using QuickLook.Common.ExtensionMethods;
|
using QuickLook.Common.ExtensionMethods;
|
||||||
|
|
||||||
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
namespace QuickLook.Plugin.ImageViewer.AnimatedImage
|
||||||
{
|
{
|
||||||
internal class GIFAnimationProvider : AnimationProvider
|
internal class GifAnimationProvider : AnimationProvider
|
||||||
{
|
{
|
||||||
private readonly List<FrameInfo> _decodedFrames;
|
private Bitmap _frame;
|
||||||
private readonly int _lastRenderedFrameIndex;
|
private BitmapSource _frameSource;
|
||||||
private readonly DrawingGroup renderedFrame;
|
private bool _isPlaying;
|
||||||
|
|
||||||
public GIFAnimationProvider(string path) : base(path)
|
public GifAnimationProvider(string path, Dispatcher uiDispatcher) : base(path, uiDispatcher)
|
||||||
{
|
{
|
||||||
var decoder = new GifBitmapDecoder(new Uri(path), BitmapCreateOptions.PreservePixelFormat,
|
_frame = (Bitmap) Image.FromFile(path);
|
||||||
BitmapCacheOption.OnLoad);
|
_frameSource = _frame.ToBitmapSource();
|
||||||
|
|
||||||
_decodedFrames = new List<FrameInfo>(decoder.Frames.Count);
|
Animator = new Int32Animation(0, 1, new Duration(TimeSpan.FromMilliseconds(50)))
|
||||||
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))))
|
|
||||||
{
|
{
|
||||||
RepeatBehavior = RepeatBehavior.Forever
|
RepeatBehavior = RepeatBehavior.Forever
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override DrawingImage GetRenderedFrame(int index)
|
public override void Dispose()
|
||||||
{
|
{
|
||||||
for (var i = _lastRenderedFrameIndex + 1; i < index; i++)
|
if (_frame == null)
|
||||||
MakeFrame(renderedFrame, _decodedFrames[i], i > 0 ? _decodedFrames[i - 1] : null);
|
return;
|
||||||
|
|
||||||
MakeFrame(
|
ImageAnimator.StopAnimate(_frame, OnFrameChanged);
|
||||||
renderedFrame,
|
_frame.Dispose();
|
||||||
_decodedFrames[index],
|
|
||||||
index > 0 ? _decodedFrames[index - 1] : null);
|
|
||||||
|
|
||||||
var di=new DrawingImage(renderedFrame);
|
|
||||||
di.Freeze();
|
|
||||||
|
|
||||||
return di;
|
_frame = null;
|
||||||
|
_frameSource = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region private methods
|
public override ImageSource GetRenderedFrame(int index)
|
||||||
|
|
||||||
private static void MakeFrame(
|
|
||||||
DrawingGroup renderedFrame,
|
|
||||||
FrameInfo currentFrame,
|
|
||||||
FrameInfo previousFrame)
|
|
||||||
{
|
{
|
||||||
if (previousFrame == null)
|
if (!_isPlaying)
|
||||||
renderedFrame.Children.Clear();
|
|
||||||
else
|
|
||||||
switch (previousFrame.DisposalMethod)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
Height = frame.PixelHeight,
|
|
||||||
Left = 0,
|
|
||||||
Top = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (frame.Metadata is BitmapMetadata metadata)
|
|
||||||
{
|
|
||||||
const string delayQuery = "/grctlext/Delay";
|
|
||||||
const string disposalQuery = "/grctlext/Disposal";
|
|
||||||
const string widthQuery = "/imgdesc/Width";
|
|
||||||
const string heightQuery = "/imgdesc/Height";
|
|
||||||
const string leftQuery = "/imgdesc/Left";
|
|
||||||
const string topQuery = "/imgdesc/Top";
|
|
||||||
|
|
||||||
var delay = metadata.GetQueryOrNull<ushort>(delayQuery);
|
|
||||||
if (delay.HasValue)
|
|
||||||
frameInfo.Delay = TimeSpan.FromMilliseconds(10 * delay.Value);
|
|
||||||
|
|
||||||
var disposal = metadata.GetQueryOrNull<byte>(disposalQuery);
|
|
||||||
if (disposal.HasValue)
|
|
||||||
frameInfo.DisposalMethod = (FrameDisposalMethod) disposal.Value;
|
|
||||||
|
|
||||||
var width = metadata.GetQueryOrNull<ushort>(widthQuery);
|
|
||||||
if (width.HasValue)
|
|
||||||
frameInfo.Width = width.Value;
|
|
||||||
|
|
||||||
var height = metadata.GetQueryOrNull<ushort>(heightQuery);
|
|
||||||
if (height.HasValue)
|
|
||||||
frameInfo.Height = height.Value;
|
|
||||||
|
|
||||||
var left = metadata.GetQueryOrNull<ushort>(leftQuery);
|
|
||||||
if (left.HasValue)
|
|
||||||
frameInfo.Left = left.Value;
|
|
||||||
|
|
||||||
var top = metadata.GetQueryOrNull<ushort>(topQuery);
|
|
||||||
if (top.HasValue)
|
|
||||||
frameInfo.Top = top.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (NotSupportedException)
|
|
||||||
{
|
{
|
||||||
|
_isPlaying = true;
|
||||||
|
ImageAnimator.Animate(_frame, OnFrameChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
return frameInfo;
|
return _frameSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
private void OnFrameChanged(object sender, EventArgs e)
|
||||||
|
|
||||||
#region structs
|
|
||||||
|
|
||||||
private class FrameInfo
|
|
||||||
{
|
{
|
||||||
public BitmapSource Frame { get; set; }
|
ImageAnimator.UpdateFrames();
|
||||||
public FrameDisposalMethod DisposalMethod { get; set; }
|
_frameSource = _frame.ToBitmapSource();
|
||||||
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; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private enum FrameDisposalMethod
|
|
||||||
{
|
|
||||||
Unspecified = 0,
|
|
||||||
Combine = 1,
|
|
||||||
RestoreBackground = 2,
|
|
||||||
RestorePrevious = 3
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
|
|
||||||
#region extensions
|
|
||||||
|
|
||||||
public static class Extensions
|
|
||||||
{
|
|
||||||
public static T? GetQueryOrNull<T>(this BitmapMetadata metadata, string query)
|
|
||||||
where T : struct
|
|
||||||
{
|
|
||||||
if (metadata.ContainsQuery(query))
|
|
||||||
{
|
|
||||||
var value = metadata.GetQuery(query);
|
|
||||||
if (value != null)
|
|
||||||
return (T) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
// Copyright © 2017 Paddy Xu
|
// Copyright © 2018 Paddy Xu
|
||||||
//
|
//
|
||||||
// This file is part of QuickLook program.
|
// This file is part of QuickLook program.
|
||||||
//
|
//
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// Copyright © 2017 Paddy Xu
|
// Copyright © 2018 Paddy Xu
|
||||||
//
|
//
|
||||||
// This file is part of QuickLook program.
|
// This file is part of QuickLook program.
|
||||||
//
|
//
|
||||||
@@ -84,6 +84,7 @@ namespace QuickLook.Plugin.ImageViewer
|
|||||||
|
|
||||||
public void Cleanup()
|
public void Cleanup()
|
||||||
{
|
{
|
||||||
|
_ip?.Dispose();
|
||||||
_ip = null;
|
_ip = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -79,7 +79,7 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="AnimatedImage\AnimatedImage.cs" />
|
<Compile Include="AnimatedImage\AnimatedImage.cs" />
|
||||||
<None Include="AnimatedImage\APNGAnimationProvider.cs" />
|
<None Include="AnimatedImage\APNGAnimationProvider.cs" />
|
||||||
<Compile Include="AnimatedImage\GIFAnimationProvider.cs" />
|
<Compile Include="AnimatedImage\GifAnimationProvider.cs" />
|
||||||
<Compile Include="AnimatedImage\AnimationProvider.cs" />
|
<Compile Include="AnimatedImage\AnimationProvider.cs" />
|
||||||
<None Include="AnimatedImage\ImageMagickProvider.cs" />
|
<None Include="AnimatedImage\ImageMagickProvider.cs" />
|
||||||
<Compile Include="exiv2\Meta.cs" />
|
<Compile Include="exiv2\Meta.cs" />
|
||||||
|
@@ -19,7 +19,7 @@
|
|||||||
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/BLANK_LINES_AROUND_PROPERTY/@EntryValue">0</s:Int64>
|
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/BLANK_LINES_AROUND_PROPERTY/@EntryValue">0</s:Int64>
|
||||||
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
|
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderRegionName/@EntryValue"></s:String>
|
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderRegionName/@EntryValue"></s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Copyright © $CURRENT_YEAR$ $USER_NAME$
|
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Copyright © $CURRENT_YEAR$ Paddy Xu
|
||||||

|

|
||||||
This file is part of $SOLUTION$ program.
|
This file is part of $SOLUTION$ program.
|
||||||

|

|
||||||
|
Reference in New Issue
Block a user