Ejemplo n.º 1
0
    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);
        }
    }
Ejemplo n.º 2
0
    private void Read(Stream stream, IEnumerable <GifExtension> controlExtensions, bool metadataOnly)
    {
        //Note: at this point, the label (0x01) has already been read
        var bytes = new byte[13];

        stream.ReadAll(bytes, 0, bytes.Length);

        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 = GifHelpers.ReadDataBlocks(stream, metadataOnly);

        Text       = Encoding.ASCII.GetString(dataBytes);
        Extensions = controlExtensions.ToList().AsReadOnly();
    }
Ejemplo n.º 3
0
    private void Read(Stream stream)
    {
        // Note: at this point, the label (0xFE) has already been read
        var bytes = GifHelpers.ReadDataBlocks(stream, false);

        if (bytes != null)
        {
            Text = Encoding.ASCII.GetString(bytes);
        }
    }
Ejemplo n.º 4
0
 private async Task<Stream> GetIndexStreamAsync(GifFrame frame, CancellationToken cancellationToken)
 {
     var data = frame.ImageData;
     cancellationToken.ThrowIfCancellationRequested();
     _sourceStream.Seek(data.CompressedDataStartOffset, SeekOrigin.Begin);
     using (var ms = new MemoryStream(_indexStreamBuffer))
     {
         await GifHelpers.CopyDataBlocksToStreamAsync(_sourceStream, ms, cancellationToken).ConfigureAwait(false);
     }
     var lzwStream = new LzwDecompressStream(_indexStreamBuffer, data.LzwMinimumCodeSize);
     return lzwStream;
 }
Ejemplo n.º 5
0
    private void Read(Stream stream, IEnumerable <GifExtension> controlExtensions, bool metadataOnly)
    {
        //Note: at this point, the Image Separator (0x2C) has already been read
        Descriptor = GifImageDescriptor.ReadImageDescriptor(stream);

        if (Descriptor.HasLocalColorTable)
        {
            LocalColorTable = GifHelpers.ReadColorTable(stream, Descriptor.LocalColorTableSize);
        }

        ImageData  = GifImageData.ReadImageData(stream, metadataOnly);
        Extensions = controlExtensions.ToList().AsReadOnly();
    }
Ejemplo n.º 6
0
    private void Read(Stream stream, bool metadataOnly)
    {
        Header = GifHeader.ReadHeader(stream);

        if (Header.LogicalScreenDescriptor.HasGlobalColorTable)
        {
            GlobalColorTable = GifHelpers.ReadColorTable(stream, Header.LogicalScreenDescriptor.GlobalColorTableSize);
        }

        ReadFrames(stream, metadataOnly);

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

        RepeatCount = netscapeExtension != null?GifHelpers.GetRepeatCount(netscapeExtension) : (ushort)1;
    }
        private static async Task DecompressAllFramesAsync(string path)
        {
            using var fileStream = File.OpenRead(path);
            var gif = await GifDataStream.ReadAsync(fileStream);

            for (int i = 0; i < gif.Frames.Count; i++)
            {
                var frame = gif.Frames[i];
                fileStream.Seek(frame.ImageData.CompressedDataStartOffset, SeekOrigin.Begin);
                using var ms = new MemoryStream();
                await GifHelpers.CopyDataBlocksToStreamAsync(fileStream, ms);

                using var lzwStream    = new LzwDecompressStream(ms.GetBuffer(), frame.ImageData.LzwMinimumCodeSize);
                using var indOutStream = File.OpenWrite($"{path}.{i}.ind");
                await lzwStream.CopyToAsync(indOutStream);
            }
        }
Ejemplo n.º 8
0
    private void Read(Stream stream)
    {
        Signature = GifHelpers.ReadString(stream, 3);

        if (Signature != "GIF")
        {
            throw GifHelpers.InvalidSignatureException(Signature);
        }

        Version = GifHelpers.ReadString(stream, 3);

        if (Version != "87a" && Version != "89a")
        {
            throw GifHelpers.UnsupportedVersionException(Version);
        }

        LogicalScreenDescriptor = GifLogicalScreenDescriptor.ReadLogicalScreenDescriptor(stream);
    }
        private static async Task TestLzwDecompressionAsync(string path)
        {
            using (var fileStream = File.OpenRead(path))
            {
                var gif = await GifDataStream.ReadAsync(fileStream);

                var firstFrame = gif.Frames[0];
                fileStream.Seek(firstFrame.ImageData.CompressedDataStartOffset, SeekOrigin.Begin);
                using (var ms = new MemoryStream())
                {
                    await GifHelpers.CopyDataBlocksToStreamAsync(fileStream, ms);

                    using (var lzwStream = new LzwDecompressStream(ms.GetBuffer(), firstFrame.ImageData.LzwMinimumCodeSize))
                        using (var indOutStream = File.OpenWrite(path + ".ind"))
                        {
                            await lzwStream.CopyToAsync(indOutStream);
                        }
                }
            }
        }
Ejemplo n.º 10
0
    private void Read(Stream stream)
    {
        // Note: at this point, the label (0xFF) has already been read
        var bytes = new byte[12];

        stream.ReadAll(bytes, 0, bytes.Length);
        BlockSize = bytes[0]; // should always be 11

        if (BlockSize != 11)
        {
            throw GifHelpers.InvalidBlockSizeException("Application Extension", 11, BlockSize);
        }

        ApplicationIdentifier = Encoding.ASCII.GetString(bytes, 1, 8);
        var authCode = new byte[3];

        Array.Copy(bytes, 9, authCode, 0, 3);
        AuthenticationCode = authCode;
        Data = GifHelpers.ReadDataBlocks(stream, false);
    }
    private void Read(Stream stream)
    {
        // Note: at this point, the label (0xF9) has already been read
        var bytes = new byte[6];

        stream.ReadAll(bytes, 0, bytes.Length);
        BlockSize = bytes[0]; // should always be 4

        if (BlockSize != 4)
        {
            throw GifHelpers.InvalidBlockSizeException("Graphic Control Extension", 4, BlockSize);
        }

        var packedFields = bytes[1];

        DisposalMethod    = (packedFields & 0x1C) >> 2;
        UserInput         = (packedFields & 0x02) != 0;
        HasTransparency   = (packedFields & 0x01) != 0;
        Delay             = BitConverter.ToUInt16(bytes, 2) * 10; // milliseconds
        TransparencyIndex = bytes[4];
    }
Ejemplo n.º 12
0
        private async Task <Stream> GetIndexStreamAsync(GifFrame frame, CancellationToken cancellationToken)
        {
            GifImageData data = frame.ImageData;

            cancellationToken.ThrowIfCancellationRequested();
            this._sourceStream.Seek(data.CompressedDataStartOffset, SeekOrigin.Begin);
            MemoryStream ms = new MemoryStream(this._indexStreamBuffer);

            try
            {
                await GifHelpers.CopyDataBlocksToStreamAsync(this._sourceStream, (Stream)ms, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                if (ms != null)
                {
                    ((IDisposable)ms).Dispose();
                }
            }
            ms = null;
            return((Stream) new LzwDecompressStream(this._indexStreamBuffer, (int)data.LzwMinimumCodeSize));
        }
Ejemplo n.º 13
0
        private async Task CreateGifAsync(bool isBranded)
        {
            BitmapImage bitmap;
            var         images = await GifHelpers.LoadImagesAsync(isBranded, ResultsEmojis);

            //create the gif
            if (images.Count > 0)
            {
                bitmap = await GifHelpers.CreateGifBitmapImageAsync(isBranded, images, ResultsEmojis);

                if (isBranded)
                {
                    brandedGif.Source = bitmap;
                }
                else
                {
                    Gif.Source = bitmap;
                }
            }
            else //no gif was created due to lack of recorded images
            {
                //hide what should have been included with the gif
                if (isBranded)
                {
                    brandedGif.Visibility = Visibility.Collapsed;
                }
                else
                {
                    Gif.Visibility = Visibility.Collapsed;
                    OopsEmojiGifReplacement.Visibility = Visibility.Visible;
                    BestMomentsText.Visibility         = Visibility.Collapsed;
                    Emoji8Logo.Visibility = Visibility.Collapsed;
                    ShareButton.IsEnabled = false;
                }
            }
        }
Ejemplo n.º 14
0
    public static GifBlock ReadBlock(Stream stream, IEnumerable <GifExtension> controlExtensions, bool metadataOnly)
    {
        var blockId = stream.ReadByte();

        if (blockId < 0)
        {
            throw GifHelpers.UnexpectedEndOfStreamException();
        }

        switch (blockId)
        {
        case GifExtension.ExtensionIntroducer:
            return(GifExtension.ReadExtension(stream, controlExtensions, metadataOnly));

        case GifFrame.ImageSeparator:
            return(GifFrame.ReadFrame(stream, controlExtensions, metadataOnly));

        case GifTrailer.TrailerByte:
            return(GifTrailer.ReadTrailer());

        default:
            throw GifHelpers.UnknownBlockTypeException(blockId);
        }
    }
Ejemplo n.º 15
0
 private void Read(Stream stream, bool metadataOnly)
 {
     LzwMinimumCodeSize = (byte)stream.ReadByte();
     CompressedData     = GifHelpers.ReadDataBlocks(stream, metadataOnly);
 }