Menu

[231fd6]: / GIFFile.cs  Maximize  Restore  History

Download this file

202 lines (169 with data), 6.2 kB

  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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Drawing;
using GIF_Viewer.GifComponents;
using GIF_Viewer.GifComponents.Enums;
using GIF_Viewer.Utils;
namespace GIF_Viewer
{
/// <summary>
/// Represents a GIF file
/// </summary>
public class GifFile : IDisposable
{
/// <summary>
/// Path to the GIF file
/// </summary>
public string GifPath = "";
/// <summary>
/// Image object representing the current GIF
/// </summary>
public Image Gif;
/// <summary>
/// The decoder holding the information about the currently loaded .gif file
/// </summary>
private GifDecoder _gifDecoded;
/// <summary>
/// Whether there is a GIF file currently loaded on this GIFFile object
/// </summary>
public bool Loaded { get; private set; }
/// <summary>
/// Whether the GIF file is playing
/// </summary>
public bool Playing = false;
/// <summary>
/// Gets the ammount of frames on this gif
/// </summary>
public int FrameCount { get; private set; }
/// <summary>
/// Gets or sets the current frame being displayed
/// </summary>
public int CurrentFrame
{
get => _currentFrame;
set => SetCurrentFrame(value);
}
/// <summary>
/// Intervals (in ms) between each frame
/// </summary>
public int[] Intervals;
/// <summary>
/// Gets the current frame interval (in milliseconds)
/// </summary>
public int CurrentInterval => _currentInterval;
/// <summary>
/// Whether the GIF file should loop
/// </summary>
public bool CanLoop;
/// <summary>
/// Gets the Width of this GIF file
/// </summary>
public int Width { get; private set; }
/// <summary>
/// Gets the Height of this GIF file
/// </summary>
public int Height { get; private set; }
/// <summary>
/// Current frame interval (in milliseconds)
/// </summary>
private int _currentInterval;
/// <summary>
/// The current frame being displayed
/// </summary>
private int _currentFrame;
/// <summary>
/// Disposes of this GIF file
/// </summary>
public void Dispose()
{
Gif.Dispose();
_gifDecoded.Dispose();
_gifDecoded = null;
}
/// <summary>
/// Applies the memory settings to the currently loaded gif file
/// </summary>
public void ApplyMemorySettings()
{
if (_gifDecoded == null || !Loaded)
return;
_gifDecoded.MaxMemoryForBuffer = Settings.Instance.MaxBufferMemory * 1024 * 1024;
_gifDecoded.MaxMemoryForKeyframes = Settings.Instance.MaxKeyframeMemory * 1024 * 1024;
_gifDecoded.MaxKeyframeReach = Settings.Instance.MaxKeyframeReach;
_gifDecoded.ApplyMemoryFields();
}
/// <summary>
/// Loads this GIF file's parameters from the given GIF file
/// </summary>
/// <param name="path">The gif to load the parameters from</param>
public void LoadFromPath(string path)
{
Loaded = false;
// Set the path
GifPath = path;
// Decode the gif file
_gifDecoded = new GifDecoder(path);
_gifDecoded.Decode();
if (_gifDecoded.ConsolidatedState != ErrorState.Ok)
{
_gifDecoded.Dispose();
return;
}
// Get information from the gif file
Width = _gifDecoded.LogicalScreenDescriptor.LogicalScreenSize.Width;
Height = _gifDecoded.LogicalScreenDescriptor.LogicalScreenSize.Height;
_currentFrame = 0;
FrameCount = _gifDecoded.FrameCount;
// Get frame intervals
Intervals = new int[FrameCount];
for (int i = 0; i < FrameCount; i++)
{
Intervals[i] = _gifDecoded.GetDelayForFrame(i) * 10;
}
// Get whether this GIF loops over:
CanLoop = (_gifDecoded.NetscapeExtension != null && _gifDecoded.NetscapeExtension.LoopCount == 0);
// Force load of the first frame
Image img = _gifDecoded[0].TheImage;
Gif = new Bitmap(img.Width, img.Height);
FastBitmap.CopyPixels((Bitmap)_gifDecoded[_currentFrame].TheImage, (Bitmap)Gif);
Loaded = true;
ApplyMemorySettings();
}
/// <summary>
/// Returns an interval in ms for the given frame
/// </summary>
/// <param name="frame">The frame to get the interval of</param>
/// <returns>The interval for the frame, in ms</returns>
public int GetIntervalForFrame(int frame)
{
return (Intervals[frame] == 0 ? 1 : Intervals[frame]);
}
/// <summary>
/// Gets the interval in ms for the current frame of this GIF
/// </summary>
/// <returns>The interval in ms for the current frame of this GIF</returns>
public int GetIntervalForCurrentFrame()
{
return GetIntervalForFrame(CurrentFrame);
}
/// <summary>
/// Changes the current frame of this GIF file, changing the GIF file's active frame in the process
/// </summary>
/// <param name="currentFrame">The new current frame</param>
public void SetCurrentFrame(int currentFrame)
{
if (_currentFrame == currentFrame)
return;
_currentFrame = currentFrame;
_currentInterval = Intervals[currentFrame];
FastBitmap.CopyPixels((Bitmap)_gifDecoded[currentFrame].TheImage, (Bitmap)Gif);
}
/// <summary>
/// Returns the frame count of this Gif file
/// </summary>
/// <returns>The frame count of this Gif file</returns>
public int GetFrameCount()
{
return FrameCount;
}
}
}