示例#1
0
        public static Gif DecodeParallelExample()
        {
            var bytes     = File.ReadAllBytes(Path);
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var decodeProgress = new DecodeProgress();

            Gif.DecodeParallel(bytes, progress =>
            {
                Console.WriteLine("Decode progress: {0}/{1}", progress.Progress, progress.FrameCount);
                decodeProgress = progress;
            });

            while (decodeProgress.Exception == null && !decodeProgress.Completed)
            {
                Thread.Sleep(100);
            }

            if (decodeProgress.Exception != null)
            {
                throw decodeProgress.Exception;
            }

            stopwatch.Stop();

            var gif = decodeProgress.Gif;

            if (gif != null)
            {
                Console.WriteLine("GIF decoded in {0:n2}s, size: {1}x{2}, frames: {3}.", stopwatch.Elapsed.TotalSeconds,
                                  gif.Frames[0].Texture.width, gif.Frames[0].Texture.height, gif.Frames.Count);
            }

            return(gif);
        }
示例#2
0
        /// <summary>
        /// Decode byte array in multiple threads.
        /// </summary>
        public static void DecodeParallel(byte[] bytes, Action <DecodeProgress> onProgress)        // TODO: Refact
        {
            if (_free)
            {
                throw new Exception("The Free version doesn't support this feature. Please consider buying the Full version of Power GIF.");
            }

            GifParser parser;

            try
            {
                parser = new GifParser(bytes);
            }
            catch (Exception e)
            {
                onProgress(new DecodeProgress {
                    Exception = e, Completed = true
                });
                return;
            }

            var decoded        = new Dictionary <ImageDescriptor, byte[]>();
            var frameCount     = parser.Blocks.Count(i => i is ImageDescriptor);
            var decodeProgress = new DecodeProgress {
                FrameCount = frameCount
            };

            for (var i = 0; i < parser.Blocks.Count; i++)
            {
                var imageDescriptor = parser.Blocks[i] as ImageDescriptor;

                if (imageDescriptor == null)
                {
                    continue;
                }

                var data = (TableBasedImageData)parser.Blocks[i + 1 + imageDescriptor.LocalColorTableFlag];

                ThreadPool.QueueUserWorkItem(context =>
                {
                    if (decodeProgress.Completed || decodeProgress.Exception != null)
                    {
                        return;
                    }

                    byte[] colorIndexes;

                    try
                    {
                        colorIndexes = LzwDecoder.Decode(data.ImageData, data.LzwMinimumCodeSize);
                    }
                    catch (Exception e)
                    {
                        decodeProgress.Exception = e;
                        decodeProgress.Completed = true;
                        onProgress(decodeProgress);
                        return;
                    }

                    lock (decoded)
                    {
                        decoded.Add(imageDescriptor, colorIndexes);
                        decodeProgress.Progress++;

                        if (decoded.Count == frameCount)
                        {
                            try
                            {
                                decodeProgress.Gif       = CompleteDecode(parser, decoded);
                                decodeProgress.Completed = true;
                            }
                            catch (Exception e)
                            {
                                decodeProgress.Exception = e;
                                decodeProgress.Completed = true;
                            }
                        }

                        onProgress(decodeProgress);
                    }
                });
            }
        }