This repository has been archived on 2023-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
PainterlyUNO/Matrix App/adds/GifWriter.cs

204 lines
8.2 KiB
C#
Raw Normal View History

2021-06-09 16:43:27 +00:00
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Matrix_App
{
/// <summary>
/// Creates a GIF using .Net GIF encoding and additional animation headers.
/// </summary>
public class GifWriter : IDisposable
{
#region Fields
2021-06-11 11:36:55 +00:00
private const long SourceGlobalColorInfoPosition = 10, SourceImageBlockPosition = 789;
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
private readonly object syncLock = new object();
private readonly BinaryWriter writer;
private bool firstFrame = true;
2021-06-09 16:43:27 +00:00
#endregion
/// <summary>
/// Creates a new instance of GifWriter.
/// </summary>
2021-06-11 11:36:55 +00:00
/// <param name="outStream">The <see cref="Stream"/> to output the Gif to.</param>
/// <param name="defaultFrameDelay">Default Delay between consecutive frames... FrameRate = 1000 / DefaultFrameDelay.</param>
/// <param name="repeat">No of times the Gif should repeat... -1 not to repeat, 0 to repeat indefinitely.</param>
public GifWriter(Stream outStream, int defaultFrameDelay = 500, int repeat = 0)
2021-06-09 16:43:27 +00:00
{
2021-06-11 11:36:55 +00:00
if (outStream == null)
throw new ArgumentNullException(nameof(outStream));
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
if (defaultFrameDelay <= 0)
throw new ArgumentOutOfRangeException(nameof(defaultFrameDelay));
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
if (repeat < -1)
throw new ArgumentOutOfRangeException(nameof(repeat));
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
writer = new BinaryWriter(outStream);
this.DefaultFrameDelay = defaultFrameDelay;
this.Repeat = repeat;
2021-06-09 16:43:27 +00:00
}
/// <summary>
/// Creates a new instance of GifWriter.
/// </summary>
2021-06-11 11:36:55 +00:00
/// <param name="fileName">The path to the file to output the Gif to.</param>
/// <param name="defaultFrameDelay">Default Delay between consecutive frames... FrameRate = 1000 / DefaultFrameDelay.</param>
/// <param name="repeat">No of times the Gif should repeat... -1 not to repeat, 0 to repeat indefinitely.</param>
public GifWriter(string fileName, int defaultFrameDelay = 500, int repeat = -1)
: this(new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read), defaultFrameDelay, repeat) { }
2021-06-09 16:43:27 +00:00
#region Properties
/// <summary>
/// Gets or Sets the Default Width of a Frame. Used when unspecified.
/// </summary>
public int DefaultWidth { get; set; }
/// <summary>
/// Gets or Sets the Default Height of a Frame. Used when unspecified.
/// </summary>
public int DefaultHeight { get; set; }
/// <summary>
/// Gets or Sets the Default Delay in Milliseconds.
/// </summary>
public int DefaultFrameDelay { get; set; }
/// <summary>
/// The Number of Times the Animation must repeat.
/// -1 indicates no repeat. 0 indicates repeat indefinitely
/// </summary>
public int Repeat { get; }
#endregion
/// <summary>
/// Adds a frame to this animation.
/// </summary>
2021-06-11 11:36:55 +00:00
/// <param name="image">The image to add</param>
/// <param name="delay">Delay in Milliseconds between this and last frame... 0 = <see cref="DefaultFrameDelay"/></param>
public void WriteFrame(Image image, int delay = 0)
2021-06-09 16:43:27 +00:00
{
2021-06-11 11:36:55 +00:00
lock (syncLock)
2021-06-09 16:43:27 +00:00
using (var gifStream = new MemoryStream())
{
2021-06-11 11:36:55 +00:00
image.Save(gifStream, ImageFormat.Gif);
2021-06-09 16:43:27 +00:00
// Steal the global color table info
2021-06-11 11:36:55 +00:00
if (firstFrame)
InitHeader(gifStream, writer, image.Width, image.Height);
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
WriteGraphicControlBlock(gifStream, writer, delay == 0 ? DefaultFrameDelay : delay);
WriteImageBlock(gifStream, writer, !firstFrame, 0, 0, image.Width, image.Height);
2021-06-09 16:43:27 +00:00
}
2021-06-11 11:36:55 +00:00
if (firstFrame)
firstFrame = false;
2021-06-09 16:43:27 +00:00
}
#region Write
2021-06-11 11:36:55 +00:00
private void InitHeader(Stream sourceGif, BinaryWriter writer, int width, int height)
2021-06-09 16:43:27 +00:00
{
// File Header
2021-06-11 11:36:55 +00:00
writer.Write("GIF".ToCharArray()); // File type
writer.Write("89a".ToCharArray()); // File Version
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
writer.Write((short)(DefaultWidth == 0 ? width : DefaultWidth)); // Initial Logical Width
writer.Write((short)(DefaultHeight == 0 ? height : DefaultHeight)); // Initial Logical Height
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
sourceGif.Position = SourceGlobalColorInfoPosition;
writer.Write((byte)sourceGif.ReadByte()); // Global Color Table Info
writer.Write((byte)0); // Background Color Index
writer.Write((byte)0); // Pixel aspect ratio
WriteColorTable(sourceGif, writer);
2021-06-09 16:43:27 +00:00
// App Extension Header for Repeating
if (Repeat == -1)
return;
2021-06-11 11:36:55 +00:00
writer.Write(unchecked((short)0xff21)); // Application Extension Block Identifier
writer.Write((byte)0x0b); // Application Block Size
writer.Write("NETSCAPE2.0".ToCharArray()); // Application Identifier
writer.Write((byte)3); // Application block length
writer.Write((byte)1);
writer.Write((short)Repeat); // Repeat count for images.
writer.Write((byte)0); // terminator
2021-06-09 16:43:27 +00:00
}
2021-06-11 11:36:55 +00:00
static void WriteColorTable(Stream sourceGif, BinaryWriter writer)
2021-06-09 16:43:27 +00:00
{
2021-06-11 11:36:55 +00:00
sourceGif.Position = 13; // Locating the image color table
2021-06-09 16:43:27 +00:00
var colorTable = new byte[768];
2021-06-11 11:36:55 +00:00
sourceGif.Read(colorTable, 0, colorTable.Length);
writer.Write(colorTable, 0, colorTable.Length);
2021-06-09 16:43:27 +00:00
}
2021-06-11 11:36:55 +00:00
static void WriteGraphicControlBlock(Stream sourceGif, BinaryWriter writer, int frameDelay)
2021-06-09 16:43:27 +00:00
{
2021-06-11 11:36:55 +00:00
sourceGif.Position = 781; // Locating the source GCE
2021-06-09 16:43:27 +00:00
var blockhead = new byte[8];
2021-06-11 11:36:55 +00:00
sourceGif.Read(blockhead, 0, blockhead.Length); // Reading source GCE
writer.Write(unchecked((short)0xf921)); // Identifier
writer.Write((byte)0x04); // Block Size
writer.Write((byte)(blockhead[3] & 0xf7 | 0x08)); // Setting disposal flag
writer.Write((short)(frameDelay / 10)); // Setting frame delay
writer.Write(blockhead[6]); // Transparent color index
writer.Write((byte)0); // Terminator
2021-06-09 16:43:27 +00:00
}
2021-06-11 11:36:55 +00:00
static void WriteImageBlock(Stream sourceGif, BinaryWriter writer, bool includeColorTable, int x, int y, int width, int height)
2021-06-09 16:43:27 +00:00
{
2021-06-11 11:36:55 +00:00
sourceGif.Position = SourceImageBlockPosition; // Locating the image block
2021-06-09 16:43:27 +00:00
var header = new byte[11];
2021-06-11 11:36:55 +00:00
sourceGif.Read(header, 0, header.Length);
writer.Write(header[0]); // Separator
writer.Write((short)x); // Position X
writer.Write((short)y); // Position Y
writer.Write((short)width); // Width
writer.Write((short)height); // Height
if (includeColorTable) // If first frame, use global color table - else use local
2021-06-09 16:43:27 +00:00
{
2021-06-11 11:36:55 +00:00
sourceGif.Position = SourceGlobalColorInfoPosition;
writer.Write((byte)(sourceGif.ReadByte() & 0x3f | 0x80)); // Enabling local color table
WriteColorTable(sourceGif, writer);
2021-06-09 16:43:27 +00:00
}
2021-06-11 11:36:55 +00:00
else writer.Write((byte)(header[9] & 0x07 | 0x07)); // Disabling local color table
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
writer.Write(header[10]); // LZW Min Code Size
2021-06-09 16:43:27 +00:00
// Read/Write image data
2021-06-11 11:36:55 +00:00
sourceGif.Position = SourceImageBlockPosition + header.Length;
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
var dataLength = sourceGif.ReadByte();
2021-06-09 16:43:27 +00:00
while (dataLength > 0)
{
var imgData = new byte[dataLength];
2021-06-11 11:36:55 +00:00
sourceGif.Read(imgData, 0, dataLength);
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
writer.Write((byte)dataLength);
writer.Write(imgData, 0, dataLength);
dataLength = sourceGif.ReadByte();
2021-06-09 16:43:27 +00:00
}
2021-06-11 11:36:55 +00:00
writer.Write((byte)0); // Terminator
2021-06-09 16:43:27 +00:00
}
#endregion
/// <summary>
/// Frees all resources used by this object.
/// </summary>
public void Dispose()
{
// Complete File
2021-06-11 11:36:55 +00:00
writer.Write((byte)0x3b); // File Trailer
2021-06-09 16:43:27 +00:00
2021-06-11 11:36:55 +00:00
writer.BaseStream.Dispose();
writer.Dispose();
2021-06-09 16:43:27 +00:00
}
}
}