Ejemplo n.º 1
0
        private static void ReadImage(GifStream stream, GifImage gifImage, List <GraphicEx> graphics,
                                      int frameCount)
        {
            var imageDescriptor = stream.GetImageDescriptor();
            var frame           = new GifFrame {
                ImageDescriptor = imageDescriptor, LocalColorTable = gifImage.GlobalColorTable
            };

            if (imageDescriptor.LocalColorTableFlag)
            {
                frame.LocalColorTable = stream.ReadByte(imageDescriptor.LocalColorTableSize * 3);
            }

            var lzwDecoder = new LZWDecoder(stream);
            var dataSize   = stream.Read();

            frame.ColorDepth = dataSize;
            var pixel = lzwDecoder.DecodeImageData(imageDescriptor.Width, imageDescriptor.Height, dataSize);

            frame.IndexedPixel = pixel;
            var blockSize = stream.Read();

            _ = new DataStruct(blockSize, stream);
            var graphicEx = graphics[frameCount];

            frame.GraphicExtension = graphicEx;
            frame.Image            = GetImageFromPixel(pixel, imageDescriptor.Width, imageDescriptor.Height, frame.Palette,
                                                       imageDescriptor.InterlaceFlag);
            gifImage.Frames.Add(frame);
        }
Ejemplo n.º 2
0
 internal DataStruct(int blockSize, GifStream streamHelper)
 {
     BlockSize = (byte)blockSize;
     if (BlockSize > 0)
     {
         Data = streamHelper.ReadByte(BlockSize);
     }
 }
Ejemplo n.º 3
0
        internal DataStruct(int blockSize, Stream stream)
        {
            var streamHelper = new GifStream(stream);

            BlockSize = (byte)blockSize;
            if (BlockSize > 0)
            {
                Data = streamHelper.ReadByte(BlockSize);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     对gif图像文件进行解码
        /// </summary>
        /// <param name="gifPath">gif文件路径</param>
        internal static GifImage Decode(string gifPath)
        {
            using (var stream = new GifStream(new FileStream(gifPath, FileMode.Open)))
            {
                var gifImage   = new GifImage();
                var graphics   = new List <GraphicEx>();
                var frameCount = 0;

                //读取文件头

                //开始6字节是GIF署名 文件版本号,GIF署名 是“GIF”,文件版本号为 "87a"或"89a"
                //87a 是 1987 版,89a 是 1989 提出的,所以就需要跳过 6 字节
                gifImage.Header = stream.ReadString(6);
                //读取逻辑屏幕标示符
                gifImage.LogicalScreenDescriptor = stream.GetLogicalScreenDescriptor();
                if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
                {
                    //读取全局颜色列表
                    gifImage.GlobalColorTable =
                        stream.ReadByte(gifImage.LogicalScreenDescriptor.GlobalColorTableSize * 3);
                }

                int nextFlag;
                while ((nextFlag = stream.Read()) != GifExtensions.Terminator)
                {
                    if (nextFlag == GifExtensions.ImageLabel)
                    {
                        ReadImage(stream, gifImage, graphics, frameCount);
                        frameCount++;
                    }
                    else if (nextFlag == GifExtensions.ExtensionIntroducer)
                    {
                        var gcl = stream.Read();
                        switch (gcl)
                        {
                        case GifExtensions.GraphicControlLabel:
                        {
                            var graphicEx = stream.GetGraphicControlExtension();
                            graphics.Add(graphicEx);
                            break;
                        }

                        case GifExtensions.CommentLabel:
                        {
                            var comment = stream.GetCommentEx();
                            gifImage.CommentExtensions.Add(comment);
                            break;
                        }

                        case GifExtensions.ApplicationExtensionLabel:
                        {
                            var applicationEx = stream.GetApplicationEx();
                            gifImage.ApplictionExtensions.Add(applicationEx);
                            break;
                        }

                        case GifExtensions.PlainTextLabel:
                        {
                            var textEx = stream.GetPlainTextEx();
                            gifImage.PlainTextExtensions.Add(textEx);
                            break;
                        }
                        }
                    }
                    else if (nextFlag == GifExtensions.EndIntroducer)
                    {
                        //到了文件尾
                        break;
                    }
                }

                return(gifImage);
            }
        }