Example #1
0
        public async Task ParseAsync(Stream stream, bool skipTypeIdentifier = false, bool ignoreImageData = false)
        {
            if (stream != null)
            {
                GIFBitmap previousBitmap = null;
                GIFBitmap currentBitmap  = null;

                GIFBitmapDecoder       decoder = new GIFBitmapDecoder();
                GIFDecoderStreamReader reader  = new GIFDecoderStreamReader(stream);

                StartParsing();

                GIFHeader header = await GIFHeader.CreateHeaderAsync(reader, skipTypeIdentifier).ConfigureAwait(false);

                currentBitmap = await GIFBitmap.CreateBitmapAsync(reader, header, decoder, previousBitmap, ignoreImageData).ConfigureAwait(false);

                while (currentBitmap != null)
                {
                    AddBitmap(header, currentBitmap, ignoreImageData);
                    previousBitmap = currentBitmap;
                    currentBitmap  = await GIFBitmap.CreateBitmapAsync(reader, header, decoder, previousBitmap, ignoreImageData).ConfigureAwait(false);
                }

                FinishedParsing();
            }
            else
            {
                throw new ArgumentNullException(nameof(stream));
            }
        }
Example #2
0
        async Task ParseImageDescriptorAsync(GIFDecoderStreamReader stream, GIFBitmapDecoder decoder, GIFBitmap previousBitmap, bool ignoreImageData)
        {
            await ParseGIFBitmapHeaderAsync(stream).ConfigureAwait(false);

            if (IsTransparent)
            {
                ColorTable.SetTransparency(TransparencyIndex);
            }

            DataPosition = stream.CurrentPosition;

            if (!ignoreImageData)
            {
                // Decode LZW data stream.
                await decoder.DecodeAsync(stream, _header.Width, _header.Height).ConfigureAwait(false);

                // Compose bitmap from decoded data stream.
                decoder.Compose(_header, this, previousBitmap);

                // Consume block terminator.
                await stream.SkipBlockAsync().ConfigureAwait(false);
            }
            else
            {
                // Read pass variable length LZW data stream.
                // First byte is LZW code size followed by data blocks repeated until block terminator.
                stream.Read();
                await stream.SkipBlockAsync().ConfigureAwait(false);
            }

            if (IsTransparent)
            {
                ColorTable.ResetTransparency();
            }
        }
Example #3
0
        public static async Task <GIFBitmap> CreateBitmapAsync(GIFDecoderStreamReader stream, GIFHeader header, GIFBitmapDecoder decoder, GIFBitmap previousBitmap, bool ignoreImageData = false)
        {
            GIFBitmap currentBitmap = null;
            bool      haveImage     = false;
            bool      done          = false;

            while (!done)
            {
                int blockCode = stream.Read();
                if (blockCode == -1)
                {
                    currentBitmap = null;
                    break;
                }

                switch (blockCode)
                {
                case GIFBlockCodes.ImageSeparator:
                    if (currentBitmap == null)
                    {
                        currentBitmap = new GIFBitmap(header);
                    }
                    await currentBitmap.ParseImageDescriptorAsync(stream, decoder, previousBitmap, ignoreImageData).ConfigureAwait(false);

                    haveImage = true;
                    done      = true;
                    break;

                case GIFBlockCodes.Extension:
                    if (currentBitmap == null)
                    {
                        currentBitmap = new GIFBitmap(header);
                    }
                    await currentBitmap.ParseExtensionAsync(stream).ConfigureAwait(false);

                    break;

                case GIFBlockCodes.Trailer:
                    done = true;
                    if (!haveImage)
                    {
                        currentBitmap = null;
                    }
                    break;

                default:
                    break;
                }
            }

            return(currentBitmap);
        }