Beispiel #1
0
        private static void ReadImage(StreamHelper streamHelper, Stream fs, GifImage gifImage, List <GraphicEx> graphics, int frameCount)
        {
            ImageDescriptor imgDes = streamHelper.GetImageDescriptor(fs);
            GifFrame        frame  = new GifFrame();

            frame.ImageDescriptor = imgDes;
            frame.LocalColorTable = gifImage.GlobalColorTable;

            if (imgDes.LctFlag)
            {
                frame.LocalColorTable = streamHelper.ReadByte(imgDes.LctSize * 3);
            }

            LZWDecoder lzwDecoder = new LZWDecoder(fs);
            int        dataSize   = streamHelper.Read();

            frame.ColorDepth = dataSize;

            byte[] pixels = lzwDecoder.DecodeImageData(imgDes.Width, imgDes.Height, dataSize, imgDes.InterlaceFlag);

            frame.IndexedPixel = pixels;

            int        blockSize = streamHelper.Read();
            DataStruct data      = new DataStruct(blockSize, fs);
            GraphicEx  graphicEx = null;

            if (graphics.Count > 0)
            {
                graphicEx = graphics[frameCount];
            }

            frame.GraphicExtension = graphicEx;

            // Handle delta GIF images
            WriteableBitmap prev;

            if ((gifImage.LogicalScreenDescriptor.Packed == 245) && (frameCount > 0))
            {
                prev = gifImage.Frames[frameCount - 1].Image;
            }
            else
            {
                prev = null;
            }

            WriteableBitmap img = GetImageFromPixel(pixels, frame.Palette, imgDes.InterlaceFlag, imgDes.Width, imgDes.Height, prev);

            frame.Image = img;
            gifImage.Frames.Add(frame);
        }
Beispiel #2
0
        /// <summary>
        /// 从文件数据流中读取图形控制扩展(Graphic Control Extension)
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public GraphicEx GetGraphicControlExtension(Stream stream)
        {
            GraphicEx gex       = new GraphicEx();
            int       blockSize = Read();

            if (blockSize != GraphicEx.BlockSize)
            {
                throw new Exception("数据格式错误!");
            }
            gex.Packed           = (byte)Read();
            gex.TransparencyFlag = (gex.Packed & 0x01) == 1;
            gex.DisposalMethod   = (gex.Packed & 0x1C) >> 2;
            gex.Delay            = ReadShort();
            gex.TranIndex        = (byte)Read();
            Read();
            return(gex);
        }
Beispiel #3
0
        public static GifImage Decode(Stream fs)
        {
            StreamHelper     streamHelper = null;
            GifImage         gifImage     = new GifImage();
            List <GraphicEx> graphics     = new List <GraphicEx>();
            int frameCount = 0;

            streamHelper = new StreamHelper(fs);

            gifImage.Header = streamHelper.ReadString(6);
            gifImage.LogicalScreenDescriptor = streamHelper.GetLCD(fs);

            if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
            {
                gifImage.GlobalColorTable = streamHelper.ReadByte(gifImage.LogicalScreenDescriptor.GlobalColorTableSize * 3);
            }

            int nextFlag = streamHelper.Read();

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

                    case GifExtensions.CommentLabel:
                    {
                        CommentEx comment = streamHelper.GetCommentEx(fs);
                        gifImage.CommentExtensions.Add(comment);
                        break;
                    }

                    case GifExtensions.ApplicationExtensionLabel:
                    {
                        ApplicationEx applicationEx = streamHelper.GetApplicationEx(fs);
                        gifImage.ApplictionExtensions.Add(applicationEx);
                        break;
                    }

                    case GifExtensions.PlainTextLabel:
                    {
                        PlainTextEx textEx = streamHelper.GetPlainTextEx(fs);
                        gifImage.PlainTextEntensions.Add(textEx);
                        break;
                    }
                    }
                }
                else if (nextFlag == GifExtensions.EndIntroducer)
                {
                    break;
                }
                nextFlag = streamHelper.Read();
            }
            return(gifImage);
        }