-
Notifications
You must be signed in to change notification settings - Fork 22
/
GifInstance.cs
154 lines (122 loc) · 5.11 KB
/
GifInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using AvaloniaGif.Decoding;
using JetBrains.Annotations;
namespace AvaloniaGif
{
internal class GifInstance : IDisposable
{
public IterationCount IterationCount { get; set; }
public bool AutoStart { get; private set; } = true;
private readonly GifDecoder _gifDecoder;
private readonly WriteableBitmap _targetBitmap;
private TimeSpan _totalTime;
private readonly List<TimeSpan> _frameTimes;
private uint _iterationCount;
private int _currentFrameIndex;
private uint _totalFrameCount;
private readonly List<ulong> _colorTableIdList;
public CancellationTokenSource CurrentCts { get; }
public GifInstance(object newValue)
{
CurrentCts = new CancellationTokenSource();
var currentStream = newValue switch
{
Stream s => s,
Uri u => GetStreamFromUri(u),
string str => GetStreamFromString(str),
_ => throw new InvalidDataException("Unsupported source object")
};
if (!currentStream.CanSeek)
throw new InvalidDataException("The provided stream is not seekable.");
if (!currentStream.CanRead)
throw new InvalidOperationException("Can't read the stream provided.");
if (currentStream.CanSeek)
{
currentStream.Seek(0, SeekOrigin.Begin);
}
_gifDecoder = new GifDecoder(currentStream, CurrentCts.Token);
var pixSize = new PixelSize(_gifDecoder.Header.Dimensions.Width, _gifDecoder.Header.Dimensions.Height);
_targetBitmap = new WriteableBitmap(pixSize, new Vector(96, 96), PixelFormat.Bgra8888, AlphaFormat.Opaque);
GifPixelSize = pixSize;
_totalTime = TimeSpan.Zero;
_frameTimes = _gifDecoder.Frames.Select(frame =>
{
_totalTime = _totalTime.Add(frame.FrameDelay);
return _totalTime;
}).ToList();
_gifDecoder.RenderFrame(0, _targetBitmap);
// Save the color table cache ID's to refresh them on cache while
// // the image is either stopped/paused.
// _colorTableIdList = _gifDecoder.Frames
// .Where(p => p.IsLocalColorTableUsed)
// .Select(p => p.LocalColorTableCacheID)
// .ToList();
// if (_gifDecoder.Header.HasGlobalColorTable)
// _colorTableIdList.Add(_gifDecoder.Header.GlobalColorTableCacheID);
}
private Stream GetStreamFromString(string str)
{
if (!Uri.TryCreate(str, UriKind.RelativeOrAbsolute, out var res))
{
throw new InvalidCastException("The string provided can't be converted to URI.");
}
return GetStreamFromUri(res);
}
private Stream GetStreamFromUri(Uri uri)
{
var uriString = uri.OriginalString.Trim();
if (!uriString.StartsWith("resm") && !uriString.StartsWith("avares"))
throw new InvalidDataException(
"The URI provided is not currently supported.");
var assetLocator = AvaloniaLocator.Current.GetService<IAssetLoader>();
if (assetLocator is null)
throw new InvalidDataException(
"The resource URI was not found in the current assembly.");
return assetLocator.Open(uri);
}
public PixelSize GifPixelSize { get; }
public void Dispose()
{
CurrentCts.Cancel();
_targetBitmap?.Dispose();
}
[CanBeNull]
public WriteableBitmap ProcessFrameTime(TimeSpan stopwatchElapsed)
{
if (!IterationCount.IsInfinite && _iterationCount > IterationCount.Value)
{
return null;
}
if (CurrentCts.IsCancellationRequested)
{
return null;
}
var timeModulus = TimeSpan.FromTicks(stopwatchElapsed.Ticks % _totalTime.Ticks);
var targetFrame = _frameTimes.LastOrDefault(x => x <= timeModulus);
var currentFrame = _frameTimes.IndexOf(targetFrame);
if (currentFrame == -1) currentFrame = 0;
if (_currentFrameIndex != currentFrame)
{
// We skipped too much frames in between render updates
// so clear the frame and redraw the previous one.
if (currentFrame - _currentFrameIndex > 1)
{
}
_currentFrameIndex = currentFrame;
_gifDecoder.RenderFrame(_currentFrameIndex, _targetBitmap);
_totalFrameCount++;
if (!IterationCount.IsInfinite && _totalFrameCount % _frameTimes.Count == 0)
_iterationCount++;
}
return _targetBitmap;
}
}
}