Skip to content

Commit

Permalink
Fixes for v2.22.1
Browse files Browse the repository at this point in the history
  • Loading branch information
NickeManarin committed Mar 13, 2020
1 parent 9c7503a commit 648927a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
3 changes: 2 additions & 1 deletion ScreenToGif/ScreenToGif.csproj
Expand Up @@ -49,7 +49,8 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>UWP</DefineConstants>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>
Expand Down
57 changes: 42 additions & 15 deletions ScreenToGif/Util/GifskiInterop.cs
Expand Up @@ -128,10 +128,13 @@ internal enum GifskiError
OtherError = 15
}

private double _timeStamp = 0;

private delegate IntPtr NewDelegate(GifskiSettings settings);
private delegate GifskiError AddPngFrameDelegate(IntPtr handle, uint index, [MarshalAs(UnmanagedType.LPUTF8Str)] string path, ushort delay);
private delegate GifskiError AddRgbaFrameDelegate(IntPtr handle, uint index, uint width, uint height, IntPtr pixels, ushort delay);
//private delegate GifskiError AddRgbaFrameDelegate(IntPtr handle, uint index, uint width, uint height, IntPtr pixels, ushort delay);
private delegate GifskiError AddRgbFrameDelegate(IntPtr handle, uint index, uint width, uint bytesPerRow, uint height, IntPtr pixels, ushort delay);
private delegate GifskiError AddRgb2FrameDelegate(IntPtr handle, uint frameNumber, uint width, uint bytesPerRow, uint height, IntPtr pixels, double timestamp);

private delegate GifskiError SetFileOutputDelegate(IntPtr handle, [MarshalAs(UnmanagedType.LPUTF8Str)] string path);
private delegate GifskiError FinishDelegate(IntPtr handle);
Expand All @@ -143,7 +146,8 @@ internal enum GifskiError
private readonly NewDelegate _new;
private readonly AddPngFrameDelegate _addPngFrame;
private readonly AddRgbFrameDelegate _addRgbFrame;
private readonly AddRgbaFrameDelegate _addRgbaFrame;
private readonly AddRgb2FrameDelegate _addRgb2Frame;
//private readonly AddRgbaFrameDelegate _addRgbaFrame;

private readonly SetFileOutputDelegate _setFileOutput;
private readonly FinishDelegate _finish;
Expand All @@ -161,20 +165,16 @@ public GifskiInterop()

switch (info.Length)
{
case 494_080:
Version = new Version(0, 10, 2);
break;

case 502_720:
Version = new Version(0, 10, 1);
Version = new Version(0, 10, 2);
break;

case 502_208:
Version = new Version(0, 9, 3);
break;

default:
Version = new Version(0,0);
Version = new Version(0, 0);
break;
}

Expand All @@ -184,8 +184,12 @@ public GifskiInterop()

_new = (NewDelegate)FunctionLoader.LoadFunction<NewDelegate>(UserSettings.All.GifskiLocation, "gifski_new");
_addPngFrame = (AddPngFrameDelegate)FunctionLoader.LoadFunction<AddPngFrameDelegate>(UserSettings.All.GifskiLocation, "gifski_add_frame_png_file");
_addRgbaFrame = (AddRgbaFrameDelegate)FunctionLoader.LoadFunction<AddRgbaFrameDelegate>(UserSettings.All.GifskiLocation, "gifski_add_frame_rgba");
_addRgbFrame = (AddRgbFrameDelegate)FunctionLoader.LoadFunction<AddRgbFrameDelegate>(UserSettings.All.GifskiLocation, "gifski_add_frame_rgb");
//_addRgbaFrame = (AddRgbaFrameDelegate)FunctionLoader.LoadFunction<AddRgbaFrameDelegate>(UserSettings.All.GifskiLocation, "gifski_add_frame_rgba");

if (Version.Major == 0 && Version.Minor < 10)
_addRgbFrame = (AddRgbFrameDelegate)FunctionLoader.LoadFunction<AddRgbFrameDelegate>(UserSettings.All.GifskiLocation, "gifski_add_frame_rgb");
else
_addRgb2Frame = (AddRgb2FrameDelegate)FunctionLoader.LoadFunction<AddRgb2FrameDelegate>(UserSettings.All.GifskiLocation, "gifski_add_frame_rgb");

if (Version.Major == 0 && Version.Minor < 9)
{
Expand All @@ -209,7 +213,7 @@ internal IntPtr Start(int quality, bool looped = true, bool fast = false)
return _new(new GifskiSettings((byte)quality, looped, fast));
}

internal GifskiError AddFrame(IntPtr handle, uint index, string path, int delay)
internal GifskiError AddFrame(IntPtr handle, uint index, string path, int delay, bool isLast = false)
{
if (Version.Major == 0 && Version.Minor < 9)
return _addPngFrame(handle, index, path, (ushort)(delay / 10));
Expand All @@ -226,12 +230,30 @@ internal GifskiError AddFrame(IntPtr handle, uint index, string path, int delay)
var pinnedBuffer = GCHandle.Alloc(util.Pixels, GCHandleType.Pinned);
var address = pinnedBuffer.AddrOfPinnedObject();

//Load the cursor shape into the buffer.
var result = AddFramePixels(handle, index, (uint)util.Width, (uint)bytesPerRow, (uint)util.Height, address, (ushort)delay);

GifskiError result;

if (Version.Major == 0 && Version.Minor >= 10)
{
result = AddFrame2Pixels(handle, index, (uint)util.Width, (uint)bytesPerRow, (uint)util.Height, address, _timeStamp);

//As a dirty fix for Gifski 0.10.2, the last frame must be duplicated to preserve the timings.
if (isLast)
{
_timeStamp += ((delay / 1000d) / 2d);
result = AddFrame2Pixels(handle, index + 1, (uint)util.Width, (uint)bytesPerRow, (uint)util.Height, address, _timeStamp);
}

//Frames can't be more than 1 seconds apart. TODO: Add support for dealing with this issue.
_timeStamp += (delay / 1000d);
}
else
{
//Normal delay.
result = AddFramePixels(handle, index, (uint)util.Width, (uint)bytesPerRow, (uint)util.Height, address, (ushort)delay);
}

//The buffer must be unpinned, to free resources.
pinnedBuffer.Free();

util.UnlockBitsWithoutCommit();

return result;
Expand All @@ -242,6 +264,11 @@ internal GifskiError AddFramePixels(IntPtr handle, uint index, uint width, uint
return _addRgbFrame(handle, index, width, bytesPerRow, height, pixels, (ushort)(delay / 10));
}

internal GifskiError AddFrame2Pixels(IntPtr handle, uint frameNumber, uint width, uint bytesPerRow, uint height, IntPtr pixels, double timestamp)
{
return _addRgb2Frame(handle, frameNumber, width, bytesPerRow, height, pixels, timestamp);
}

internal GifskiError EndAdding(IntPtr handle)
{
if (Version.Major == 0 && Version.Minor < 9)
Expand Down
2 changes: 2 additions & 0 deletions ScreenToGif/Windows/Editor.xaml.cs
Expand Up @@ -7542,6 +7542,8 @@ private void DelayAsync(DelayModel model, bool forAll = false, bool ignoreUi = f

UpdateProgress(count++);
}

Project.Persist();
}

private void DelayCallback(IAsyncResult ar)
Expand Down
2 changes: 1 addition & 1 deletion ScreenToGif/Windows/Other/Encoder.xaml.cs
Expand Up @@ -755,7 +755,7 @@ private async void Encode(List<FrameInfo> frames, int id, Parameters param, Canc

Update(id, i, string.Format(processing, i));

res = gifski.AddFrame(handle, (uint)i, frames[i].Path, frames[i].Delay);
res = gifski.AddFrame(handle, (uint)i, frames[i].Path, frames[i].Delay, i + 1 == frames.Count);

if (res != GifskiInterop.GifskiError.Ok)
throw new Exception("Error while adding frames with Gifski. " + res, new Win32Exception(res.ToString())) { HelpLink = $"Result:\n\r{Marshal.GetLastWin32Error()}" };
Expand Down

0 comments on commit 648927a

Please sign in to comment.