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,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()
{
}
}