예제 #1
0
        private async Task ReadFramesAsync(Stream stream)
        {
            List <GifFrame>     frames            = new List <GifFrame>();
            List <GifExtension> controlExtensions = new List <GifExtension>();
            List <GifExtension> specialExtensions = new List <GifExtension>();

            while (true)
            {
                try
                {
                    var block = await GifBlock.ReadAsync(stream, controlExtensions).ConfigureAwait(false);

                    if (block.Kind == GifBlockKind.GraphicRendering)
                    {
                        controlExtensions = new List <GifExtension>();
                    }

                    if (block is GifFrame)
                    {
                        frames.Add((GifFrame)block);
                    }
                    else if (block is GifExtension)
                    {
                        var extension = (GifExtension)block;
                        switch (extension.Kind)
                        {
                        case GifBlockKind.Control:
                            controlExtensions.Add(extension);
                            break;

                        case GifBlockKind.SpecialPurpose:
                            specialExtensions.Add(extension);
                            break;

                            // Just discard plain text extensions for now, since we have no use for it
                        }
                    }
                    else if (block is GifTrailer)
                    {
                        break;
                    }
                }
                // Follow the same approach as Firefox:
                // If we find extraneous data between blocks, just assume the stream
                // was successfully terminated if we have some successfully decoded frames
                // https://dxr.mozilla.org/firefox/source/modules/libpr0n/decoders/gif/nsGIFDecoder2.cpp#894-909
                catch (UnknownBlockTypeException) when(frames.Count > 0)
                {
                    break;
                }
            }

            this.Frames     = frames.AsReadOnly();
            this.Extensions = specialExtensions.AsReadOnly();
        }
예제 #2
0
        private async Task ReadFramesAsync(Stream stream)
        {
            List <GifFrame>     frames            = new List <GifFrame>();
            List <GifExtension> controlExtensions = new List <GifExtension>();
            List <GifExtension> specialExtensions = new List <GifExtension>();

            while (true)
            {
                var block = await GifBlock.ReadAsync(stream, controlExtensions).ConfigureAwait(false);

                if (block.Kind == GifBlockKind.GraphicRendering)
                {
                    controlExtensions = new List <GifExtension>();
                }

                if (block is GifFrame)
                {
                    frames.Add((GifFrame)block);
                }
                else if (block is GifExtension)
                {
                    var extension = (GifExtension)block;
                    switch (extension.Kind)
                    {
                    case GifBlockKind.Control:
                        controlExtensions.Add(extension);
                        break;

                    case GifBlockKind.SpecialPurpose:
                        specialExtensions.Add(extension);
                        break;

                        // Just discard plain text extensions for now, since we have no use for it
                    }
                }
                else if (block is GifTrailer)
                {
                    break;
                }
            }

            this.Frames     = frames.AsReadOnly();
            this.Extensions = specialExtensions.AsReadOnly();
        }
예제 #3
0
        private async Task ReadFramesAsync(Stream stream)
        {
            List <GifFrame>     frames            = new List <GifFrame>();
            List <GifExtension> controlExtensions = new List <GifExtension>();
            List <GifExtension> specialExtensions = new List <GifExtension>();
            GifBlock            gifBlock;

            do
            {
                gifBlock = await GifBlock.ReadAsync(stream, (IEnumerable <GifExtension>) controlExtensions).ConfigureAwait(false);

                if (gifBlock.Kind == GifBlockKind.GraphicRendering)
                {
                    controlExtensions = new List <GifExtension>();
                }
                if (gifBlock is GifFrame)
                {
                    frames.Add((GifFrame)gifBlock);
                }
                else if (gifBlock is GifExtension)
                {
                    GifExtension gifExtension = (GifExtension)gifBlock;
                    switch (gifExtension.Kind)
                    {
                    case GifBlockKind.Control:
                        controlExtensions.Add(gifExtension);
                        continue;

                    case GifBlockKind.SpecialPurpose:
                        specialExtensions.Add(gifExtension);
                        continue;

                    default:
                        continue;
                    }
                }
            }while (!(gifBlock is GifTrailer));
            this.Frames     = (IList <GifFrame>)frames.AsReadOnly();
            this.Extensions = (IList <GifExtension>)specialExtensions.AsReadOnly();
        }