public static GifExtension ReadExtension(Stream stream, IEnumerable <GifExtension> controlExtensions, bool metadataOnly) { //Note: at this point, the Extension Introducer (0x21) has already been read var label = stream.ReadByte(); if (label < 0) { throw GifHelpers.UnexpectedEndOfStreamException(); } switch (label) { case GifGraphicControlExtension.ExtensionLabel: return(GifGraphicControlExtension.ReadGraphicsControl(stream)); case GifCommentExtension.ExtensionLabel: return(GifCommentExtension.ReadComment(stream)); case GifPlainTextExtension.ExtensionLabel: return(GifPlainTextExtension.ReadPlainText(stream, controlExtensions, metadataOnly)); case GifApplicationExtension.ExtensionLabel: return(GifApplicationExtension.ReadApplication(stream)); default: throw GifHelpers.UnknownExtensionTypeException(label); } }
public static GifApplicationExtension ReadApplication(Stream stream) { var ext = new GifApplicationExtension(); ext.Read(stream); return(ext); }
public static ushort GetRepeatCount(GifApplicationExtension ext) { if (ext.Data.Length >= 3) { return(BitConverter.ToUInt16(ext.Data, 1)); } return(1); }
/// <summary> /// Initializes the module. /// </summary> public static void Initialize() { GraphicControlGifExtension.registerSelfAsGifExtension(); GifApplicationExtension.registerSelfAsGifExtension(); LoopingGifApplicationExtension.registerSelfAsAppExtension(); }
public static GifBitmapCoder ConvertStciIndexedToGif( StciIndexed aStci, UInt16 aDelay, bool aUseTransparent, int aForeshotingNumber) { var _stciPalette = aStci.Palette; var _colors = new List <Color>(StciIndexed.NUMBER_OF_COLORS); for (int i = 0; i < StciIndexed.NUMBER_OF_COLORS; i++) { _colors.Add(Color.FromRgb(_stciPalette[i * 3], _stciPalette[i * 3 + 1], _stciPalette[i * 3 + 2])); } var _palette = new BitmapPalette(_colors); var _gifCoder = new GifBitmapCoder(_palette); _gifCoder.Extensions.Add(new GifCommentExtension("Egorov A. V. for ja2.su")); Int16 _minOffsetX = Int16.MaxValue; Int16 _minOffsetY = Int16.MaxValue; int foreshotingCount = 0; foreach (var _subImage in aStci.Images) { if (_subImage.AuxData != null && _subImage.AuxData.NumberOfFrames > 0) { foreshotingCount++; } if (aForeshotingNumber > 0 && aForeshotingNumber != foreshotingCount) { continue; } var _header = _subImage.Header; _minOffsetX = Math.Min(_minOffsetX, _header.OffsetX); _minOffsetY = Math.Min(_minOffsetY, _header.OffsetY); } if (_minOffsetX < 0 || _minOffsetY < 0) { var _shiftData = new List <byte>(4); _shiftData.AddRange(BitConverter.GetBytes(_minOffsetX)); _shiftData.AddRange(BitConverter.GetBytes(_minOffsetY)); var _shiftExtension = new GifApplicationExtension(ApplicationId, _shiftData.ToArray()); _gifCoder.Extensions.Add(_shiftExtension); } foreshotingCount = 0; foreach (var _subImage in aStci.Images) { if (_subImage.AuxData != null && _subImage.AuxData.NumberOfFrames > 0) { foreshotingCount++; } if (aForeshotingNumber > 0 && aForeshotingNumber != foreshotingCount) { continue; } var _header = _subImage.Header; var _imageSource = BitmapSource.Create( _header.Width, _header.Height, 96, 96, PixelFormats.Indexed8, _palette, _subImage.ImageData, _header.Width); var _frame = BitmapFrame.Create(_imageSource); var _offsetX = _header.OffsetX; var _offsetY = _header.OffsetY; if (_minOffsetX < 0 || _minOffsetY < 0) { // GIF format suports only positive offsets _offsetX = (short)(_offsetX - _minOffsetX); _offsetY = (short)(_offsetY - _minOffsetY); } var _bf = new GifBitmapFrame(_frame, (ushort)_offsetX, (ushort)_offsetY); if (_subImage.AuxData != null) { var _auxData = new byte[AuxObjectData.SIZE]; _subImage.AuxData.Save(new MemoryStream(_auxData)); _bf.Extensions.Add(new GifApplicationExtension(ApplicationId, _auxData)); } _bf.DisposalMethod = GifFrameDisposalMethod.RestoreToBackgroundColor; _bf.UseGlobalPalette = true; _bf.UseTransparency = aUseTransparent; _bf.Delay = aDelay; if (aUseTransparent) { _bf.TransparentColorIndex = (byte)aStci.Header.TransparentColorIndex; } _gifCoder.AddFrame(_bf); } return(_gifCoder); }
public static bool IsNetscapeExtension(GifApplicationExtension ext) { return(ext.ApplicationIdentifier == "NETSCAPE" && Encoding.ASCII.GetString(ext.AuthenticationCode) == "2.0"); }