/// <summary> /// Writes the graphics control extension to the stream. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="image">The <see cref="ImageBase{TPixel}"/> to encode.</param> /// <param name="metaData">The metadata of the image or frame.</param> /// <param name="writer">The stream to write to.</param> /// <param name="transparencyIndex">The index of the color in the color palette to make transparent.</param> private void WriteGraphicalControlExtension <TPixel>(ImageBase <TPixel> image, IMetaData metaData, EndianBinaryWriter writer, int transparencyIndex) where TPixel : struct, IPixel <TPixel> { GifGraphicsControlExtension extension = new GifGraphicsControlExtension() { DisposalMethod = metaData.DisposalMethod, TransparencyFlag = transparencyIndex < 255, TransparencyIndex = transparencyIndex, DelayTime = metaData.FrameDelay }; // Write the intro. this.buffer[0] = GifConstants.ExtensionIntroducer; this.buffer[1] = GifConstants.GraphicControlLabel; this.buffer[2] = 4; writer.Write(this.buffer, 0, 3); PackedField field = default(PackedField); field.SetBits(3, 3, (int)extension.DisposalMethod); // 1-3 : Reserved, 4-6 : Disposal // TODO: Allow this as an option. field.SetBit(6, false); // 7 : User input - 0 = none field.SetBit(7, extension.TransparencyFlag); // 8: Has transparent. writer.Write(field.Byte); writer.Write((ushort)extension.DelayTime); writer.Write((byte)extension.TransparencyIndex); writer.Write(GifConstants.Terminator); }
/// <summary> /// Reads the graphic control extension. /// </summary> private void ReadGraphicalControlExtension() { this.currentStream.Read(this.buffer, 0, 6); byte packed = this.buffer[1]; this.graphicsControlExtension = new GifGraphicsControlExtension { DelayTime = BitConverter.ToInt16(this.buffer, 2), TransparencyIndex = this.buffer[4], TransparencyFlag = (packed & 0x01) == 1, DisposalMethod = (DisposalMethod)((packed & 0x1C) >> 2) }; }
/// <summary> /// Writes the graphics control extension to the stream. /// </summary> /// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam> /// <param name="image">The <see cref="ImageBase{TColor, TPacked}"/> to encode.</param> /// <param name="writer">The stream to write to.</param> /// <param name="transparencyIndex">The index of the color in the color palette to make transparent.</param> private void WriteGraphicalControlExtension <TColor, TPacked>(ImageBase <TColor, TPacked> image, EndianBinaryWriter writer, int transparencyIndex) where TColor : struct, IPackedPixel <TPacked> where TPacked : struct { // TODO: Check transparency logic. bool hasTransparent = transparencyIndex > -1; DisposalMethod disposalMethod = hasTransparent ? DisposalMethod.RestoreToBackground : DisposalMethod.Unspecified; GifGraphicsControlExtension extension = new GifGraphicsControlExtension() { DisposalMethod = disposalMethod, TransparencyFlag = hasTransparent, TransparencyIndex = transparencyIndex, DelayTime = image.FrameDelay }; // Reduce the number of writes. byte[] intro = { GifConstants.ExtensionIntroducer, GifConstants.GraphicControlLabel, 4 // Size }; writer.Write(intro); PackedField field = default(PackedField); field.SetBits(3, 3, (int)extension.DisposalMethod); // 1-3 : Reserved, 4-6 : Disposal // TODO: Allow this as an option. field.SetBit(6, false); // 7 : User input - 0 = none field.SetBit(7, extension.TransparencyFlag); // 8: Has transparent. writer.Write(field.Byte); writer.Write((ushort)extension.DelayTime); writer.Write((byte)(extension.TransparencyIndex == -1 ? 255 : extension.TransparencyIndex)); writer.Write(GifConstants.Terminator); }
/// <summary> /// Writes the graphics control extension to the stream. /// </summary> /// <typeparam name="TColor">The pixel format.</typeparam> /// <param name="image">The <see cref="ImageBase{TColor}"/> to encode.</param> /// <param name="metaData">The metadata of the image or frame.</param> /// <param name="writer">The stream to write to.</param> /// <param name="transparencyIndex">The index of the color in the color palette to make transparent.</param> private void WriteGraphicalControlExtension <TColor>(ImageBase <TColor> image, IMetaData metaData, EndianBinaryWriter writer, int transparencyIndex) where TColor : struct, IPackedPixel, IEquatable <TColor> { // TODO: Check transparency logic. bool hasTransparent = transparencyIndex < 255; DisposalMethod disposalMethod = hasTransparent ? DisposalMethod.RestoreToBackground : DisposalMethod.Unspecified; GifGraphicsControlExtension extension = new GifGraphicsControlExtension() { DisposalMethod = disposalMethod, TransparencyFlag = hasTransparent, TransparencyIndex = transparencyIndex, DelayTime = metaData.FrameDelay }; // Write the intro. this.buffer[0] = GifConstants.ExtensionIntroducer; this.buffer[1] = GifConstants.GraphicControlLabel; this.buffer[2] = 4; writer.Write(this.buffer, 0, 3); PackedField field = default(PackedField); field.SetBits(3, 3, (int)extension.DisposalMethod); // 1-3 : Reserved, 4-6 : Disposal // TODO: Allow this as an option. field.SetBit(6, false); // 7 : User input - 0 = none field.SetBit(7, extension.TransparencyFlag); // 8: Has transparent. writer.Write(field.Byte); writer.Write((ushort)extension.DelayTime); writer.Write((byte)extension.TransparencyIndex); writer.Write(GifConstants.Terminator); }