Esempio n. 1
0
 private BrushAnimator(Stream sourceStream, Uri sourceUri, GifDataStream metadata, RepeatBehavior repeatBehavior) : base(sourceStream, sourceUri, metadata, repeatBehavior)
 {
     Brush = new ImageBrush {
         ImageSource = Bitmap
     };
     RepeatBehavior = _repeatBehavior;
 }
Esempio n. 2
0
        private static async Task <Animator> CreateAsync(Stream sourceStream, Uri sourceUri, RepeatBehavior repeatBehavior, Image image)
        {
            Stream        stream   = sourceStream.AsBuffered();
            GifDataStream metadata = await GifDataStream.ReadAsync(stream);

            return(new Animator(stream, sourceUri, metadata, repeatBehavior, image));
        }
Esempio n. 3
0
        private static Dictionary <int, Animator.GifPalette> CreatePalettes(GifDataStream metadata)
        {
            Dictionary <int, Animator.GifPalette> dictionary = new Dictionary <int, Animator.GifPalette>();

            Color[] colorArray = (Color[])null;
            if (metadata.Header.LogicalScreenDescriptor.HasGlobalColorTable)
            {
                colorArray = (Color[])Enumerable.ToArray <Color>(Enumerable.Select <GifColor, Color>(metadata.GlobalColorTable, (Func <GifColor, Color>)(gc => Color.FromArgb(byte.MaxValue, gc.R, gc.G, gc.B))));
            }
            for (int index = 0; index < metadata.Frames.Count; ++index)
            {
                GifFrame frame  = metadata.Frames[index];
                Color[]  colors = colorArray;
                if (frame.Descriptor.HasLocalColorTable)
                {
                    colors = (Color[])Enumerable.ToArray <Color>(Enumerable.Select <GifColor, Color>(frame.LocalColorTable, (Func <GifColor, Color>)(gc => Color.FromArgb(byte.MaxValue, gc.R, gc.G, gc.B))));
                }
                int?transparencyIndex = new int?();
                GifGraphicControlExtension graphicControl = frame.GraphicControl;
                if (graphicControl != null && graphicControl.HasTransparency)
                {
                    transparencyIndex = new int?(graphicControl.TransparencyIndex);
                }
                dictionary[index] = new Animator.GifPalette(transparencyIndex, colors);
            }
            return(dictionary);
        }
Esempio n. 4
0
        private static WriteableBitmap CreateBitmap(GifDataStream metadata)
        {
            var desc   = metadata.Header.LogicalScreenDescriptor;
            var bitmap = new WriteableBitmap(desc.Width, desc.Height, 96, 96, PixelFormats.Bgra32, null);

            return(bitmap);
        }
Esempio n. 5
0
 private static RepeatBehavior GetRepeatBehavior(GifDataStream metadata)
 {
     if ((int)metadata.RepeatCount == 0)
     {
         return(RepeatBehavior.Forever);
     }
     return(new RepeatBehavior((double)metadata.RepeatCount));
 }
Esempio n. 6
0
 internal static async Task<TAnimator> CreateAsyncCore<TAnimator>(
     Stream sourceStream,
     Func<GifDataStream, TAnimator> create)
     where TAnimator : Animator
 {
     var metadata = await GifDataStream.ReadAsync(sourceStream);
     return create(metadata);
 }
Esempio n. 7
0
 private static RepeatBehavior GetRepeatBehaviorFromGif(GifDataStream metadata)
 {
     if (metadata.RepeatCount == 0)
     {
         return(RepeatBehavior.Forever);
     }
     return(new RepeatBehavior(metadata.RepeatCount));
 }
Esempio n. 8
0
        private static byte[] CreateIndexStreamBuffer(GifDataStream metadata, Stream stream)
        {
            long val2 = stream.Length - ((GifFrame)Enumerable.Last <GifFrame>(metadata.Frames)).ImageData.CompressedDataStartOffset;
            long num  = val2;

            if (metadata.Frames.Count > 1)
            {
                num = Math.Max(Enumerable.Max((IEnumerable <long>)Enumerable.Zip <GifFrame, GifFrame, long>((IEnumerable <GifFrame>)metadata.Frames, (IEnumerable <GifFrame>)Enumerable.Skip <GifFrame>((IEnumerable <GifFrame>)metadata.Frames, 1), (Func <GifFrame, GifFrame, long>)((f1, f2) => f2.ImageData.CompressedDataStartOffset - f1.ImageData.CompressedDataStartOffset))), val2);
            }
            return(new byte[num + 4L]);
        }
Esempio n. 9
0
        private TimingManager CreateTimingManager(GifDataStream metadata, RepeatBehavior repeatBehavior)
        {
            TimingManager timingManager = new TimingManager(repeatBehavior == new RepeatBehavior() ? Animator.GetRepeatBehavior(metadata) : repeatBehavior);

            foreach (GifFrame frame in (IEnumerable <GifFrame>)metadata.Frames)
            {
                timingManager.Add(Animator.GetFrameDelay(frame));
            }
            timingManager.Completed += new EventHandler(this.TimingManagerCompleted);
            return(timingManager);
        }
Esempio n. 10
0
        private static WriteableBitmap CreateBitmap(GifDataStream metadata)
        {
            var desc = metadata.Header.LogicalScreenDescriptor;
#if WPF
            var bitmap = new WriteableBitmap(desc.Width, desc.Height, 96, 96, PixelFormats.Bgra32, null);
#elif WINRT
            var bitmap = new WriteableBitmap(desc.Width, desc.Height);
#else
            #error Not implemented
#endif
            return bitmap;
        }
Esempio n. 11
0
 internal Animator(Stream sourceStream, Uri sourceUri, GifDataStream metadata, RepeatBehavior repeatBehavior)
 {
     _sourceStream = sourceStream;
     _sourceUri = sourceUri;
     _metadata = metadata;
     _palettes = CreatePalettes(metadata);
     _bitmap = CreateBitmap(metadata);
     var desc = metadata.Header.LogicalScreenDescriptor;
     _stride = 4 * ((desc.Width * 32 + 31) / 32);
     _previousBackBuffer = new byte[desc.Height * _stride];
     _indexStreamBuffer = CreateIndexStreamBuffer(metadata, _sourceStream);
     _timingManager = CreateTimingManager(metadata, repeatBehavior);
 }
Esempio n. 12
0
        private TimingManager CreateTimingManager(GifDataStream metadata, RepeatBehavior repeatBehavior)
        {
            var actualRepeatBehavior = GetActualRepeatBehavior(metadata, repeatBehavior);

            var manager = new TimingManager(actualRepeatBehavior);

            foreach (var frame in metadata.Frames)
            {
                manager.Add(GetFrameDelay(frame));
            }

            manager.Completed += TimingManagerCompleted;
            return(manager);
        }
Esempio n. 13
0
        internal static async Task <TAnimator> CreateAsyncCore <TAnimator>(
            Stream sourceStream,
            Func <GifDataStream, TAnimator> create)
            where TAnimator : Animator
        {
            if (!sourceStream.CanSeek)
            {
                throw new ArgumentException("The stream is not seekable");
            }
            sourceStream.Seek(0, SeekOrigin.Begin);
            var metadata = await GifDataStream.ReadAsync(sourceStream);

            return(create(metadata));
        }
Esempio n. 14
0
        private Animator(Stream sourceStream, Uri sourceUri, GifDataStream metadata, RepeatBehavior repeatBehavior, Image image)
        {
            //this.\u002Ector();
            this._sourceStream = sourceStream;
            this._sourceUri    = sourceUri;
            this._metadata     = metadata;
            this._image        = image;
            this._palettes     = Animator.CreatePalettes(metadata);
            this._bitmap       = Animator.CreateBitmap(metadata);
            GifLogicalScreenDescriptor screenDescriptor = metadata.Header.LogicalScreenDescriptor;

            this._stride             = 4 * ((screenDescriptor.Width * 32 + 31) / 32);
            this._previousBackBuffer = new byte[screenDescriptor.Height * this._stride];
            this._indexStreamBuffer  = Animator.CreateIndexStreamBuffer(metadata, this._sourceStream);
            this._timingManager      = this.CreateTimingManager(metadata, repeatBehavior);
        }
Esempio n. 15
0
        private static byte[] CreateIndexStreamBuffer(GifDataStream metadata, Stream stream)
        {
            // Find the size of the largest frame pixel data
            // (ignoring the fact that we include the next frame's header)

            long lastSize = stream.Length - metadata.Frames.Last().ImageData.CompressedDataStartOffset;
            long maxSize = lastSize;
            if (metadata.Frames.Count > 1)
            {
                var sizes = metadata.Frames.Zip(metadata.Frames.Skip(1),
                    (f1, f2) => f2.ImageData.CompressedDataStartOffset - f1.ImageData.CompressedDataStartOffset);
                maxSize = Math.Max(sizes.Max(), lastSize);
            }
            // Need 4 extra bytes so that BitReader doesn't need to check the size for every read
            return new byte[maxSize + 4];
        }
        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);
            }
        }
        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);
                        }
                }
            }
        }
Esempio n. 18
0
        private static Dictionary <int, GifPalette> CreatePalettes(GifDataStream metadata)
        {
            var palettes = new Dictionary <int, GifPalette>();

            Color[] globalColorTable = null;
            if (metadata.Header.LogicalScreenDescriptor.HasGlobalColorTable)
            {
                globalColorTable =
                    metadata.GlobalColorTable
                    .Select(gc => Color.FromArgb(0xFF, gc.R, gc.G, gc.B))
                    .ToArray();
            }

            for (int i = 0; i < metadata.Frames.Count; i++)
            {
                var frame      = metadata.Frames[i];
                var colorTable = globalColorTable;
                if (frame.Descriptor.HasLocalColorTable)
                {
                    colorTable =
                        frame.LocalColorTable
                        .Select(gc => Color.FromArgb(0xFF, gc.R, gc.G, gc.B))
                        .ToArray();
                }

                int?transparencyIndex = null;
                var gce = frame.GraphicControl;
                if (gce != null && gce.HasTransparency)
                {
                    transparencyIndex = gce.TransparencyIndex;
                }

                palettes[i] = new GifPalette(transparencyIndex, colorTable);
            }

            return(palettes);
        }
Esempio n. 19
0
 private static RepeatBehavior GetActualRepeatBehavior(GifDataStream metadata, RepeatBehavior repeatBehavior)
 {
     return(repeatBehavior == default
             ? GetRepeatBehaviorFromGif(metadata)
             : repeatBehavior);
 }
Esempio n. 20
0
 private static RepeatBehavior GetRepeatBehavior(GifDataStream metadata)
 {
     if (metadata.RepeatCount == 0)
         return RepeatBehavior.Forever;
     return new RepeatBehavior(metadata.RepeatCount);
 }
Esempio n. 21
0
 public ImageAnimator(Stream sourceStream, Uri sourceUri, GifDataStream metadata, RepeatBehavior repeatBehavior, Image image) : base(sourceStream, sourceUri, metadata, repeatBehavior)
 {
     _image = image;
     OnRepeatBehaviorChanged(); // in case the value has changed during creation
 }
Esempio n. 22
0
 public ImageAnimator(Stream sourceStream, Uri sourceUri, GifDataStream metadata, RepeatBehavior repeatBehavior, Image image) : base(sourceStream, sourceUri, metadata, repeatBehavior)
 {
     ErrorSource = image;
 }
Esempio n. 23
0
        private static WriteableBitmap CreateBitmap(GifDataStream metadata)
        {
            GifLogicalScreenDescriptor screenDescriptor = metadata.Header.LogicalScreenDescriptor;

            return(new WriteableBitmap(screenDescriptor.Width, screenDescriptor.Height));
        }