Skip to content

Commit

Permalink
Framerate adjustments for encoders v1 and v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
NickeManarin committed Oct 7, 2019
1 parent 81f78b3 commit 5585ae5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
21 changes: 15 additions & 6 deletions ScreenToGif/ImageUtil/Gif/Encoder/GifFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ public class GifFile : IDisposable
/// </summary>
private int ColorTableSize { get; set; }

private int cumulativeTime { get; set; } = 0;
private int cumulativePrevDelay { get; set; } = 0;
/// <summary>
/// Cumulative non adjusted time.
/// </summary>
private int OrganicTime { get; set; }

/// <summary>
/// Adjusted and rounded off time.
/// </summary>
private int AdjustedTime { get; set; }

#endregion

Expand All @@ -106,10 +113,6 @@ public void AddFrame(string path, Int32Rect rect, int delay = 66)
{
CurrentTransparentColor = TransparentColor;

cumulativeTime += delay;
delay = (int)Math.Round((cumulativeTime > delay ? cumulativeTime - cumulativePrevDelay*10 : delay) / 10.0f, MidpointRounding.AwayFromZero);
cumulativePrevDelay += delay;

ReadPixels(path);
//HandleTransparency();

Expand Down Expand Up @@ -270,6 +273,12 @@ private void WriteGraphicControlExtension(int delay)
//Write the packed fields.
WriteByte(ConvertToByte(bitArray));

//Calculates the delay, taking into consideration overall rounding.
OrganicTime += delay;
delay = (int)Math.Round((OrganicTime > delay ? OrganicTime - AdjustedTime * 10 : delay) / 10.0f, MidpointRounding.AwayFromZero);
AdjustedTime += delay;
//WriteShort((int)Math.Round(delay / 10.0f, MidpointRounding.AwayFromZero));

WriteShort(delay);
WriteByte(FindTransparentColorIndex()); //Transparency Index.
WriteByte(0); //Terminator.
Expand Down
21 changes: 18 additions & 3 deletions ScreenToGif/ImageUtil/Gif/LegacyEncoder/AnimatedGifEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,31 @@ public class AnimatedGifEncoder : IDisposable
/// </summary>
private int _sample = 10;

/// <summary>
/// Cumulative non adjusted time.
/// </summary>
private int _organicTime;

/// <summary>
/// Adjusted and rounded off time.
/// </summary>
private int _adjustedTime;

#endregion

/// <summary>
/// Sets the delay time between each frame, or changes it
/// for subsequent frames (applies to last frame added).
/// </summary>
/// <param name="ms">Delay time in milliseconds</param>
public void SetDelay(int ms)
/// <param name="delay">Delay time in milliseconds</param>
public void SetDelay(int delay)
{
_delay = (int)Math.Round(ms / 10.0f, MidpointRounding.AwayFromZero);
//Calculates the delay, taking into consideration overall rounding.
_organicTime += delay;
_delay = (int)Math.Round((_organicTime > delay ? _organicTime - _adjustedTime * 10 : delay) / 10.0f, MidpointRounding.AwayFromZero);
_adjustedTime += _delay;

//_delay = (int)Math.Round(delay / 10.0f, MidpointRounding.AwayFromZero);
}

/// <summary>
Expand Down

0 comments on commit 5585ae5

Please sign in to comment.