Example #1
0
        internal static async Task <GifExtension> ReadAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            int label = stream.ReadByte();

            if (label < 0)
            {
                throw GifHelpers.UnexpectedEndOfStreamException();
            }
            switch (label)
            {
            case 254:
                return((GifExtension)await GifCommentExtension.ReadAsync(stream).ConfigureAwait(false));

            case (int)byte.MaxValue:
                return((GifExtension)await GifApplicationExtension.ReadAsync(stream).ConfigureAwait(false));

            case 1:
                return((GifExtension)await GifPlainTextExtension.ReadAsync(stream, controlExtensions).ConfigureAwait(false));

            case 249:
                return((GifExtension)await GifGraphicControlExtension.ReadAsync(stream).ConfigureAwait(false));

            default:
                throw GifHelpers.UnknownExtensionTypeException(label);
            }
        }
Example #2
0
        private async Task ReadInternalAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            // Note: at this point, the label (0x01) has already been read

            byte[] bytes = new byte[13];
            await stream.ReadAllAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            BlockSize = bytes[0];
            if (BlockSize != 12)
            {
                throw GifHelpers.InvalidBlockSizeException("Plain Text Extension", 12, BlockSize);
            }

            Left                 = BitConverter.ToUInt16(bytes, 1);
            Top                  = BitConverter.ToUInt16(bytes, 3);
            Width                = BitConverter.ToUInt16(bytes, 5);
            Height               = BitConverter.ToUInt16(bytes, 7);
            CellWidth            = bytes[9];
            CellHeight           = bytes[10];
            ForegroundColorIndex = bytes[11];
            BackgroundColorIndex = bytes[12];

            var dataBytes = await GifHelpers.ReadDataBlocksAsync(stream).ConfigureAwait(false);

            Text       = GifHelpers.GetString(dataBytes);
            Extensions = controlExtensions.ToList().AsReadOnly();
        }
Example #3
0
        public static async Task <string> ReadStringAsync(Stream stream, int length)
        {
            byte[] bytes = new byte[length];
            await stream.ReadAllAsync(bytes, 0, length).ConfigureAwait(false);

            return(GifHelpers.GetString(bytes));
        }
Example #4
0
        internal new static async Task <GifExtension> ReadAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            // Note: at this point, the Extension Introducer (0x21) has already been read

            int label = stream.ReadByte();

            if (label < 0)
            {
                throw new EndOfStreamException();
            }
            switch (label)
            {
            case GifGraphicControlExtension.ExtensionLabel:
                return(await GifGraphicControlExtension.ReadAsync(stream).ConfigureAwait(false));

            case GifCommentExtension.ExtensionLabel:
                return(await GifCommentExtension.ReadAsync(stream).ConfigureAwait(false));

            case GifPlainTextExtension.ExtensionLabel:
                return(await GifPlainTextExtension.ReadAsync(stream, controlExtensions).ConfigureAwait(false));

            case GifApplicationExtension.ExtensionLabel:
                return(await GifApplicationExtension.ReadAsync(stream).ConfigureAwait(false));

            default:
                throw GifHelpers.UnknownExtensionTypeException(label);
            }
        }
Example #5
0
 public static bool IsNetscapeExtension(GifApplicationExtension ext)
 {
     if (ext.ApplicationIdentifier == "NETSCAPE")
     {
         return(GifHelpers.GetString(ext.AuthenticationCode) == "2.0");
     }
     return(false);
 }
Example #6
0
        private async Task ReadInternalAsync(Stream stream)
        {
            byte[] bytes = await GifHelpers.ReadDataBlocksAsync(stream, false).ConfigureAwait(false);

            if (bytes == null)
            {
                return;
            }
            this.Text = GifHelpers.GetString(bytes);
        }
Example #7
0
        private async Task ReadInternalAsync(Stream stream)
        {
            // Note: at this point, the label (0xFE) has already been read

            var bytes = await GifHelpers.ReadDataBlocksAsync(stream).ConfigureAwait(false);

            if (bytes != null)
            {
                Text = GifHelpers.GetString(bytes);
            }
        }
Example #8
0
        private async Task ReadInternalAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            this.Descriptor = await GifImageDescriptor.ReadAsync(stream).ConfigureAwait(false);

            if (this.Descriptor.HasLocalColorTable)
            {
                this.LocalColorTable = await GifHelpers.ReadColorTableAsync(stream, this.Descriptor.LocalColorTableSize).ConfigureAwait(false);
            }
            this.ImageData = await GifImageData.ReadAsync(stream).ConfigureAwait(false);

            this.Extensions     = (IList <GifExtension>)controlExtensions.ToList <GifExtension>().AsReadOnly();
            this.GraphicControl = this.Extensions.OfType <GifGraphicControlExtension>().FirstOrDefault <GifGraphicControlExtension>();
        }
Example #9
0
        private async Task ReadInternalAsync(Stream stream)
        {
            this.Header = await GifHeader.ReadAsync(stream).ConfigureAwait(false);

            if (this.Header.LogicalScreenDescriptor.HasGlobalColorTable)
            {
                this.GlobalColorTable = await GifHelpers.ReadColorTableAsync(stream, this.Header.LogicalScreenDescriptor.GlobalColorTableSize).ConfigureAwait(false);
            }
            await this.ReadFramesAsync(stream).ConfigureAwait(false);

            GifApplicationExtension ext = this.Extensions.OfType <GifApplicationExtension>().FirstOrDefault <GifApplicationExtension>(new Func <GifApplicationExtension, bool>(GifHelpers.IsNetscapeExtension));

            this.RepeatCount = ext != null?GifHelpers.GetRepeatCount(ext) : (ushort)1;
        }
Example #10
0
        private async Task ReadInternalAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            // Note: at this point, the Image Separator (0x2C) has already been read

            Descriptor = await GifImageDescriptor.ReadAsync(stream).ConfigureAwait(false);

            if (Descriptor.HasLocalColorTable)
            {
                LocalColorTable = await GifHelpers.ReadColorTableAsync(stream, Descriptor.LocalColorTableSize).ConfigureAwait(false);
            }
            ImageData = await GifImageData.ReadAsync(stream).ConfigureAwait(false);

            Extensions     = controlExtensions.ToList().AsReadOnly();
            GraphicControl = Extensions.OfType <GifGraphicControlExtension>().FirstOrDefault();
        }
Example #11
0
        internal static async Task <GifBlock> ReadAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            int blockId = await stream.ReadByteAsync().ConfigureAwait(false);

            if (blockId < 0)
            {
                throw new EndOfStreamException();
            }
            return(blockId switch
            {
                GifExtension.ExtensionIntroducer => await GifExtension.ReadAsync(stream, controlExtensions).ConfigureAwait(false),
                GifFrame.ImageSeparator => await GifFrame.ReadAsync(stream, controlExtensions).ConfigureAwait(false),
                GifTrailer.TrailerByte => await GifTrailer.ReadAsync().ConfigureAwait(false),
                _ => throw GifHelpers.UnknownBlockTypeException(blockId),
            });
        private async Task ReadInternalAsync(Stream stream)
        {
            byte[] bytes = new byte[12];
            await stream.ReadAllAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            this.BlockSize = (int)bytes[0];
            if (this.BlockSize != 11)
            {
                throw GifHelpers.InvalidBlockSizeException("Application Extension", 11, this.BlockSize);
            }
            this.ApplicationIdentifier = GifHelpers.GetString(bytes, 1, 8);
            byte[] numArray = new byte[3];
            Array.Copy((Array)bytes, 9, (Array)numArray, 0, 3);
            this.AuthenticationCode = numArray;
            this.Data = await GifHelpers.ReadDataBlocksAsync(stream, false).ConfigureAwait(false);
        }
Example #13
0
        private async Task ReadInternalAsync(Stream stream)
        {
            Signature = await GifHelpers.ReadStringAsync(stream, 3).ConfigureAwait(false);

            if (Signature != "GIF")
            {
                throw GifHelpers.InvalidSignatureException(Signature);
            }
            Version = await GifHelpers.ReadStringAsync(stream, 3).ConfigureAwait(false);

            if (Version != "87a" && Version != "89a")
            {
                throw GifHelpers.UnsupportedVersionException(Version);
            }
            LogicalScreenDescriptor = await GifLogicalScreenDescriptor.ReadAsync(stream).ConfigureAwait(false);
        }
Example #14
0
 internal static async Task<GifBlock> ReadAsync(Stream stream, IEnumerable<GifExtension> controlExtensions)
 {
     int blockId = await stream.ReadByteAsync().ConfigureAwait(false);
     if (blockId < 0)
         throw GifHelpers.UnexpectedEndOfStreamException();
     switch (blockId)
     {
         case GifExtension.ExtensionIntroducer:
             return await GifExtension.ReadAsync(stream, controlExtensions).ConfigureAwait(false);
         case GifFrame.ImageSeparator:
             return await GifFrame.ReadAsync(stream, controlExtensions).ConfigureAwait(false);
         case GifTrailer.TrailerByte:
             return await GifTrailer.ReadAsync().ConfigureAwait(false);
         default:
             throw GifHelpers.UnknownBlockTypeException(blockId);
     }
 }
Example #15
0
        private async Task ReadInternalAsync(Stream stream)
        {
            byte[] bytes = new byte[6];
            await stream.ReadAllAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            this.BlockSize = (int)bytes[0];
            if (this.BlockSize != 4)
            {
                throw GifHelpers.InvalidBlockSizeException("Graphic Control Extension", 4, this.BlockSize);
            }
            byte num = bytes[1];

            this.DisposalMethod    = (GifFrameDisposalMethod)(((int)num & 28) >> 2);
            this.UserInput         = ((uint)num & 2U) > 0U;
            this.HasTransparency   = ((uint)num & 1U) > 0U;
            this.Delay             = (int)BitConverter.ToUInt16(bytes, 2) * 10;
            this.TransparencyIndex = (int)bytes[4];
        }
Example #16
0
        private async Task ReadInternalAsync(Stream stream)
        {
            // Note: at this point, the label (0xFF) has already been read

            byte[] bytes = new byte[12];
            await stream.ReadAllAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            BlockSize = bytes[0]; // should always be 11
            if (BlockSize != 11)
            {
                throw GifHelpers.InvalidBlockSizeException("Application Extension", 11, BlockSize);
            }

            ApplicationIdentifier = GifHelpers.GetString(bytes, 1, 8);
            byte[] authCode = new byte[3];
            Array.Copy(bytes, 9, authCode, 0, 3);
            AuthenticationCode = authCode;
            Data = await GifHelpers.ReadDataBlocksAsync(stream).ConfigureAwait(false);
        }
Example #17
0
        private async Task ReadInternalAsync(Stream stream)
        {
            Header = await GifHeader.ReadAsync(stream).ConfigureAwait(false);

            if (Header.LogicalScreenDescriptor.HasGlobalColorTable)
            {
                GlobalColorTable = await GifHelpers.ReadColorTableAsync(stream, Header.LogicalScreenDescriptor.GlobalColorTableSize).ConfigureAwait(false);
            }
            await ReadFramesAsync(stream).ConfigureAwait(false);

            var netscapeExtension =
                Extensions
                .OfType <GifApplicationExtension>()
                .FirstOrDefault(GifHelpers.IsNetscapeExtension);

            RepeatCount = netscapeExtension != null
                ? GifHelpers.GetRepeatCount(netscapeExtension)
                : (ushort)1;
        }
Example #18
0
        private async Task ReadInternalAsync(Stream stream)
        {
            // Note: at this point, the label (0xF9) has already been read

            byte[] bytes = new byte[6];
            await stream.ReadAllAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            BlockSize = bytes[0]; // should always be 4
            if (BlockSize != 4)
            {
                throw GifHelpers.InvalidBlockSizeException("Graphic Control Extension", 4, BlockSize);
            }
            byte packedFields = bytes[1];

            DisposalMethod    = (GifFrameDisposalMethod)((packedFields & 0x1C) >> 2);
            UserInput         = (packedFields & 0x02) != 0;
            HasTransparency   = (packedFields & 0x01) != 0;
            Delay             = BitConverter.ToUInt16(bytes, 2) * 10; // milliseconds
            TransparencyIndex = bytes[4];
        }
Example #19
0
        private async Task ReadInternalAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            byte[] bytes = new byte[13];
            await stream.ReadAllAsync(bytes, 0, bytes.Length).ConfigureAwait(false);

            this.BlockSize = (int)bytes[0];
            if (this.BlockSize != 12)
            {
                throw GifHelpers.InvalidBlockSizeException("Plain Text Extension", 12, this.BlockSize);
            }
            this.Left                 = (int)BitConverter.ToUInt16(bytes, 1);
            this.Top                  = (int)BitConverter.ToUInt16(bytes, 3);
            this.Width                = (int)BitConverter.ToUInt16(bytes, 5);
            this.Height               = (int)BitConverter.ToUInt16(bytes, 7);
            this.CellWidth            = (int)bytes[9];
            this.CellHeight           = (int)bytes[10];
            this.ForegroundColorIndex = (int)bytes[11];
            this.BackgroundColorIndex = (int)bytes[12];
            this.Text                 = GifHelpers.GetString(await GifHelpers.ReadDataBlocksAsync(stream, false).ConfigureAwait(false));
            this.Extensions           = (IList <GifExtension>)controlExtensions.ToList <GifExtension>().AsReadOnly();
        }
Example #20
0
        internal static async Task <GifBlock> ReadAsync(Stream stream, IEnumerable <GifExtension> controlExtensions)
        {
            int blockId = await stream.ReadByteAsync(new CancellationToken()).ConfigureAwait(false);

            if (blockId < 0)
            {
                throw GifHelpers.UnexpectedEndOfStreamException();
            }
            switch (blockId)
            {
            case 33:
                return((GifBlock)await GifExtension.ReadAsync(stream, controlExtensions).ConfigureAwait(false));

            case 44:
                return((GifBlock)await GifFrame.ReadAsync(stream, controlExtensions).ConfigureAwait(false));

            case 59:
                return((GifBlock)await GifTrailer.ReadAsync().ConfigureAwait(false));

            default:
                throw GifHelpers.UnknownBlockTypeException(blockId);
            }
        }
Example #21
0
 private async Task ReadInternalAsync(Stream stream)
 {
     LzwMinimumCodeSize        = (byte)stream.ReadByte();
     CompressedDataStartOffset = stream.Position;
     await GifHelpers.ConsumeDataBlocksAsync(stream).ConfigureAwait(false);
 }
Example #22
0
 public static string GetString(byte[] bytes)
 {
     return(GifHelpers.GetString(bytes, 0, bytes.Length));
 }
Example #23
0
 private async Task ReadInternalAsync(Stream stream)
 {
     this.LzwMinimumCodeSize        = (byte)stream.ReadByte();
     this.CompressedDataStartOffset = stream.Position;
     byte[] numArray = await GifHelpers.ReadDataBlocksAsync(stream, true).ConfigureAwait(false);
 }