done new image viewer. RAW problem remaining.

This commit is contained in:
Paddy Xu
2018-06-15 22:35:22 +03:00
parent 395d4bbc86
commit db31458ffe
9 changed files with 225 additions and 102 deletions

View File

@@ -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,105 +16,180 @@
// 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.Linq;
using System.Threading.Tasks;
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 LibAPNG; using LibAPNG;
using QuickLook.Common.ExtensionMethods;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage namespace QuickLook.Plugin.ImageViewer.AnimatedImage
{ {
internal class APNGAnimationProvider : IAnimationProvider internal class APNGAnimationProvider : AnimationProvider
{ {
public void GetAnimator(ObjectAnimationUsingKeyFrames animator, string path) private readonly List<FrameInfo> _frames;
private readonly List<BitmapSource> _renderedFrames;
private ImageMagickProvider _imageMagickProvider;
private int _lastEffecitvePreviousPreviousFrameIndex;
public APNGAnimationProvider(string path, Dispatcher uiDispatcher) : base(path, uiDispatcher)
{ {
var decoder = new APNGBitmap(path); var decoder = new APNGBitmap(path);
if (decoder.IsSimplePNG) if (decoder.IsSimplePNG)
{ {
new ImageMagickProvider().GetAnimator(animator, path); _imageMagickProvider = new ImageMagickProvider(path, uiDispatcher);
return; return;
} }
var clock = TimeSpan.Zero; _frames = new List<FrameInfo>(decoder.Frames.Length);
var header = decoder.IHDRChunk; _renderedFrames = new List<BitmapSource>(decoder.Frames.Length);
Frame currentFrame = null; Enumerable.Repeat(0, decoder.Frames.Length).ForEach(_ => _renderedFrames.Add(null));
BitmapSource currentRenderedFrame = null;
BitmapSource previousStateRenderedFrame = null; Animator = new Int32AnimationUsingKeyFrames {RepeatBehavior = RepeatBehavior.Forever};
foreach (var nextFrame in decoder.Frames)
var wallclock = TimeSpan.Zero;
for (var i = 0; i < decoder.Frames.Length; i++)
{ {
var nextRenderedFrame = MakeNextFrame(header, nextFrame, currentFrame, currentRenderedFrame, var frame = decoder.Frames[i];
previousStateRenderedFrame);
var delay = TimeSpan.FromSeconds( _frames.Add(new FrameInfo(decoder.IHDRChunk, frame));
(double) nextFrame.fcTLChunk.DelayNum /
(nextFrame.fcTLChunk.DelayDen == 0 ? 100 : nextFrame.fcTLChunk.DelayDen));
animator.KeyFrames.Add(new DiscreteObjectKeyFrame(nextRenderedFrame, clock)); Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(i, KeyTime.FromTimeSpan(wallclock)));
clock += delay; wallclock += _frames[i].Delay;
}
// the "previous state" of a "DisposeOpPrevious" frame is its previous frame, so we do not record it
if (currentFrame != null && currentFrame.fcTLChunk.DisposeOp != DisposeOps.APNGDisposeOpPrevious)
previousStateRenderedFrame = currentRenderedFrame;
currentRenderedFrame = nextRenderedFrame;
currentFrame = nextFrame;
} }
animator.Duration = clock; public override Task<BitmapSource> GetRenderedFrame(int index)
animator.RepeatBehavior = RepeatBehavior.Forever; {
if (_imageMagickProvider != null)
return _imageMagickProvider.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;
});
} }
private static BitmapSource MakeNextFrame(IHDRChunk header, Frame nextFrame, Frame currentFrame, public override void Dispose()
BitmapSource currentRenderedFrame, BitmapSource previousStateRenderedFrame)
{ {
var fullRect = new Rect(0, 0, header.Width, header.Height); if (_imageMagickProvider != null)
var frameRect = new Rect(nextFrame.fcTLChunk.XOffset, nextFrame.fcTLChunk.YOffset, {
nextFrame.fcTLChunk.Width, nextFrame.fcTLChunk.Height); _imageMagickProvider.Dispose();
_imageMagickProvider = 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[_lastEffecitvePreviousPreviousFrameIndex];
if (_frames[index].DisposeOp != DisposeOps.APNGDisposeOpPrevious)
_lastEffecitvePreviousPreviousFrameIndex = Math.Max(_lastEffecitvePreviousPreviousFrameIndex, index);
var fs = nextFrame.GetBitmapSource();
var visual = new DrawingVisual(); var visual = new DrawingVisual();
using (var context = visual.RenderOpen()) using (var context = visual.RenderOpen())
{ {
// protect region // protect region
if (nextFrame.fcTLChunk.BlendOp == BlendOps.APNGBlendOpSource) if (currentFrame.BlendOp == BlendOps.APNGBlendOpSource)
{ {
var freeRegion = new CombinedGeometry(GeometryCombineMode.Xor, var freeRegion = new CombinedGeometry(GeometryCombineMode.Xor,
new RectangleGeometry(fullRect), new RectangleGeometry(currentFrame.FrameRect),
new RectangleGeometry(frameRect)); new RectangleGeometry(currentFrame.FrameRect));
context.PushOpacityMask( context.PushOpacityMask(
new DrawingBrush(new GeometryDrawing(Brushes.Transparent, null, freeRegion))); new DrawingBrush(new GeometryDrawing(Brushes.Transparent, null, freeRegion)));
} }
if (currentFrame != null && currentRenderedFrame != null) if (previousFrame != null)
switch (currentFrame.fcTLChunk.DisposeOp) switch (previousFrame.DisposeOp)
{ {
case DisposeOps.APNGDisposeOpNone: case DisposeOps.APNGDisposeOpNone:
// restore currentRenderedFrame if (previousRendered != null)
if (currentRenderedFrame != null) context.DrawImage(currentRenderedFrame, fullRect); context.DrawImage(previousRendered, currentFrame.FullRect);
break; break;
case DisposeOps.APNGDisposeOpPrevious: case DisposeOps.APNGDisposeOpPrevious:
// restore previousStateRenderedFrame if (previousPreviousRendered != null)
if (previousStateRenderedFrame != null) context.DrawImage(previousPreviousRendered, currentFrame.FullRect);
context.DrawImage(previousStateRenderedFrame, fullRect);
break; break;
case DisposeOps.APNGDisposeOpBackground: case DisposeOps.APNGDisposeOpBackground:
// do nothing // do nothing
break; break;
} }
// unprotect region and draw the next frame // unprotect region and draw current frame
if (nextFrame.fcTLChunk.BlendOp == BlendOps.APNGBlendOpSource) if (currentFrame.BlendOp == BlendOps.APNGBlendOpSource)
context.Pop(); context.Pop();
context.DrawImage(fs, frameRect); context.DrawImage(currentFrame.Pixels, currentFrame.FrameRect);
} }
var bitmap = new RenderTargetBitmap( var bitmap = new RenderTargetBitmap(
header.Width, header.Height, (int) currentFrame.FullRect.Width, (int) currentFrame.FullRect.Height,
Math.Floor(fs.DpiX), Math.Floor(fs.DpiY), Math.Floor(currentFrame.Pixels.DpiX), Math.Floor(currentFrame.Pixels.DpiY),
PixelFormats.Pbgra32); PixelFormats.Pbgra32);
bitmap.Render(visual); bitmap.Render(visual);
bitmap.Freeze();
return bitmap; return bitmap;
} }
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

@@ -27,9 +27,12 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
public class AnimatedImage : Image, IDisposable public class AnimatedImage : Image, IDisposable
{ {
private AnimationProvider _animation; private AnimationProvider _animation;
private bool _disposing;
public void Dispose() public void Dispose()
{ {
_disposing = true;
BeginAnimation(AnimationFrameIndexProperty, null); BeginAnimation(AnimationFrameIndexProperty, null);
Source = null; Source = null;
@@ -37,15 +40,6 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
_animation = null; _animation = null;
} }
private static void LoadFullImage(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
if (!(obj is AnimatedImage instance))
return;
instance._animation = LoadFullImageCore((Uri) ev.NewValue, instance.Dispatcher);
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation.Animator);
}
private static AnimationProvider LoadFullImageCore(Uri path, Dispatcher uiDispatcher) private static AnimationProvider LoadFullImageCore(Uri path, Dispatcher uiDispatcher)
{ {
byte[] sign; byte[] sign;
@@ -55,14 +49,14 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
sign = reader.BaseStream.Length < 4 ? new byte[] {0, 0, 0, 0} : reader.ReadBytes(4); sign = reader.BaseStream.Length < 4 ? new byte[] {0, 0, 0, 0} : reader.ReadBytes(4);
} }
AnimationProvider provider = null; AnimationProvider provider;
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, uiDispatcher); 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(path.LocalPath, uiDispatcher);
//else else
// provider = new ImageMagickProvider(); provider = new ImageMagickProvider(path.LocalPath, uiDispatcher);
return provider; return provider;
} }
@@ -106,8 +100,9 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
//var thumbnail = instance.Meta?.GetThumbnail(true); //var thumbnail = instance.Meta?.GetThumbnail(true);
//instance.Source = thumbnail; //instance.Source = thumbnail;
LoadFullImage(obj, ev); instance._animation = LoadFullImageCore((Uri) ev.NewValue, instance.Dispatcher);
instance.BeginAnimation(AnimationFrameIndexProperty, instance._animation.Animator);
instance.AnimationFrameIndex = 0; instance.AnimationFrameIndex = 0;
} }
@@ -116,9 +111,25 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
if (!(obj is AnimatedImage instance)) if (!(obj is AnimatedImage instance))
return; return;
var image = instance._animation.GetRenderedFrame((int) ev.NewValue); if (instance._disposing)
//if (!ReferenceEquals(instance.Source, image)) return;
instance.Source = image;
var task = instance._animation.GetRenderedFrame((int) ev.NewValue);
if (instance.Source == null && (int) ev.NewValue == 0) // this is the first image. Run it synchronously.
{
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 #endregion DependencyProperty

View File

@@ -16,8 +16,9 @@
// 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.Windows.Media; using System.Threading.Tasks;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading; using System.Windows.Threading;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage namespace QuickLook.Plugin.ImageViewer.AnimatedImage
@@ -34,10 +35,10 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
public string Path { get; } public string Path { get; }
public Int32Animation Animator { get; protected set; } public Int32AnimationUsingKeyFrames Animator { get; protected set; }
public abstract void Dispose(); public abstract void Dispose();
public abstract ImageSource GetRenderedFrame(int index); public abstract Task<BitmapSource> GetRenderedFrame(int index);
} }
} }

View File

@@ -17,8 +17,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows; using System.Threading.Tasks;
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 System.Windows.Threading;
@@ -37,10 +36,11 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
_frame = (Bitmap) Image.FromFile(path); _frame = (Bitmap) Image.FromFile(path);
_frameSource = _frame.ToBitmapSource(); _frameSource = _frame.ToBitmapSource();
Animator = new Int32Animation(0, 1, new Duration(TimeSpan.FromMilliseconds(50))) Animator = new Int32AnimationUsingKeyFrames {RepeatBehavior = RepeatBehavior.Forever};
{
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() public override void Dispose()
@@ -55,7 +55,7 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
_frameSource = null; _frameSource = null;
} }
public override ImageSource GetRenderedFrame(int index) public override Task<BitmapSource> GetRenderedFrame(int index)
{ {
if (!_isPlaying) if (!_isPlaying)
{ {
@@ -63,7 +63,7 @@ namespace QuickLook.Plugin.ImageViewer.AnimatedImage
ImageAnimator.Animate(_frame, OnFrameChanged); ImageAnimator.Animate(_frame, OnFrameChanged);
} }
return _frameSource; return new Task<BitmapSource>(() => _frameSource);
} }
private void OnFrameChanged(object sender, EventArgs e) private void OnFrameChanged(object sender, EventArgs e)

View File

@@ -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,25 +16,56 @@
// 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.Windows; using System.Threading.Tasks;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using ImageMagick; using ImageMagick;
using QuickLook.Plugin.ImageViewer.Exiv2;
namespace QuickLook.Plugin.ImageViewer.AnimatedImage namespace QuickLook.Plugin.ImageViewer.AnimatedImage
{ {
internal class ImageMagickProvider : IAnimationProvider internal class ImageMagickProvider : AnimationProvider
{ {
public void GetAnimator(ObjectAnimationUsingKeyFrames animator, string path) private readonly string _path;
private readonly BitmapSource _thumbnail;
public ImageMagickProvider(string path, Dispatcher uiDispatcher) : base(path, uiDispatcher)
{ {
using (var image = new MagickImage(path)) _path = path;
_thumbnail = new Meta(path).GetThumbnail(true);
Animator = new Int32AnimationUsingKeyFrames();
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0,
KeyTime.FromTimeSpan(TimeSpan.Zero))); // thumbnail/full image
if (_thumbnail != null)
Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(1,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.20)))); // full image
}
public override Task<BitmapSource> GetRenderedFrame(int index)
{
// the first image is always returns synchronously.
if (index == 0 && _thumbnail != null) return new Task<BitmapSource>(() => _thumbnail);
return new Task<BitmapSource>(() =>
{
using (var image = new MagickImage(_path))
{ {
image.AddProfile(ColorProfile.SRGB); image.AddProfile(ColorProfile.SRGB);
image.Density = new Density(Math.Floor(image.Density.X), Math.Floor(image.Density.Y)); image.Density = new Density(Math.Floor(image.Density.X), Math.Floor(image.Density.Y));
image.AutoOrient(); image.AutoOrient();
animator.KeyFrames.Add(new DiscreteObjectKeyFrame(image.ToBitmapSource(), TimeSpan.Zero)); var bs = image.ToBitmapSource();
animator.Duration = Duration.Forever; bs.Freeze();
return bs;
} }
});
}
public override void Dispose()
{
} }
} }
} }

View File

@@ -24,23 +24,25 @@
<Rectangle.Style> <Rectangle.Style>
<Style> <Style>
<Style.Triggers> <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 Property="Rectangle.Fill">
<Setter.Value> <Setter.Value>
<ImageBrush AlignmentY="Top" Viewport="0,0,32,32" RenderOptions.BitmapScalingMode="NearestNeighbor" <ImageBrush AlignmentY="Top" Viewport="0,0,32,32"
RenderOptions.BitmapScalingMode="NearestNeighbor"
ImageSource="Resources/background-b.png" ImageSource="Resources/background-b.png"
ViewportUnits="Absolute" Stretch="UniformToFill" TileMode="Tile"> ViewportUnits="Absolute" Stretch="UniformToFill" TileMode="Tile" />
</ImageBrush>
</Setter.Value> </Setter.Value>
</Setter> </Setter>
</DataTrigger> </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 Property="Rectangle.Fill">
<Setter.Value> <Setter.Value>
<ImageBrush AlignmentY="Top" Viewport="0,0,32,32" RenderOptions.BitmapScalingMode="NearestNeighbor" <ImageBrush AlignmentY="Top" Viewport="0,0,32,32"
RenderOptions.BitmapScalingMode="NearestNeighbor"
ImageSource="Resources/background.png" ImageSource="Resources/background.png"
ViewportUnits="Absolute" Stretch="UniformToFill" TileMode="Tile"> ViewportUnits="Absolute" Stretch="UniformToFill" TileMode="Tile" />
</ImageBrush>
</Setter.Value> </Setter.Value>
</Setter> </Setter>
</DataTrigger> </DataTrigger>

View File

@@ -78,10 +78,10 @@
<Link>Properties\GitVersion.cs</Link> <Link>Properties\GitVersion.cs</Link>
</Compile> </Compile>
<Compile Include="AnimatedImage\AnimatedImage.cs" /> <Compile Include="AnimatedImage\AnimatedImage.cs" />
<None Include="AnimatedImage\APNGAnimationProvider.cs" /> <Compile 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" /> <Compile Include="AnimatedImage\ImageMagickProvider.cs" />
<Compile Include="exiv2\Meta.cs" /> <Compile Include="exiv2\Meta.cs" />
<Compile Include="ImageFileHelper.cs" /> <Compile Include="ImageFileHelper.cs" />
<Compile Include="ImagePanel.xaml.cs"> <Compile Include="ImagePanel.xaml.cs">

View File

@@ -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.
// //
@@ -83,8 +83,10 @@ namespace QuickLook.Plugin.ImageViewer.Exiv2
return image.ToBitmapSource(); return image.ToBitmapSource();
var size = GetSize(); var size = GetSize();
return new TransformedBitmap(image.ToBitmapSource(), var bitmap = new TransformedBitmap(image.ToBitmapSource(),
new ScaleTransform(size.Width / image.Width, size.Height / image.Height)); new ScaleTransform(size.Width / image.Width, size.Height / image.Height));
bitmap.Freeze();
return bitmap;
} }
} }
catch (Exception) catch (Exception)

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Magick.NET-Q8-AnyCPU" version="7.4.6" targetFramework="net462" /> <package id="Magick.NET-Q8-AnyCPU" version="7.4.6" targetFramework="net462" />
</packages> </packages>