Skip to content

Commit

Permalink
Fix #78: Use Gif Disposal flag in the correct way
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed Oct 5, 2017
1 parent abd69a2 commit 333857f
Showing 1 changed file with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ public void GetAnimator(ObjectAnimationUsingKeyFrames animator, string path)
var clock = TimeSpan.Zero;
BitmapSource prevFrame = null;
FrameInfo prevInfo = null;
BitmapSource prevprevFrame = null;
foreach (var rawFrame in decoder.Frames)
{
var info = GetFrameInfo(rawFrame);
var frame = MakeFrame(
decoder.Frames[0],
rawFrame, info,
prevFrame, prevInfo);
var frame = MakeFrame(decoder.Frames[0], rawFrame, info, prevFrame, prevInfo, prevprevFrame);
prevprevFrame = prevFrame;
prevFrame = frame;
prevInfo = info;

Expand All @@ -57,16 +56,29 @@ public void GetAnimator(ObjectAnimationUsingKeyFrames animator, string path)
private static BitmapSource MakeFrame(
BitmapSource fullImage,
BitmapSource rawFrame, FrameInfo frameInfo,
BitmapSource previousFrame, FrameInfo previousFrameInfo)
BitmapSource previousFrame, FrameInfo previousFrameInfo,
BitmapSource previouspreviousFrame)
{
var visual = new DrawingVisual();
using (var context = visual.RenderOpen())
{
if (previousFrameInfo != null && previousFrame != null &&
previousFrameInfo.DisposalMethod == FrameDisposalMethod.Combine)
if (previousFrameInfo != null && previousFrame != null)
{
var fullRect = new Rect(0, 0, fullImage.PixelWidth, fullImage.PixelHeight);
context.DrawImage(previousFrame, fullRect);

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;
}
}

context.DrawImage(rawFrame, frameInfo.Rect);
Expand All @@ -84,7 +96,7 @@ private static FrameInfo GetFrameInfo(BitmapFrame frame)
var frameInfo = new FrameInfo
{
Delay = TimeSpan.FromMilliseconds(100),
DisposalMethod = FrameDisposalMethod.Replace,
DisposalMethod = FrameDisposalMethod.Unspecified,
Width = frame.PixelWidth,
Height = frame.PixelHeight,
Left = 0,
Expand Down Expand Up @@ -152,7 +164,7 @@ private class FrameInfo

private enum FrameDisposalMethod
{
Replace = 0,
Unspecified = 0,
Combine = 1,
RestoreBackground = 2,
RestorePrevious = 3
Expand Down

0 comments on commit 333857f

Please sign in to comment.