예제 #1
1
 internal DataStruct(int blockSize,Stream stream)
 {
     StreamHelper streamHelper = new StreamHelper(stream);
     _blockSize = (byte)blockSize;
     if (_blockSize > 0)
     {
         _data = streamHelper.ReadByte(_blockSize);
     }
 }
예제 #2
0
 internal static void Encode(GifImage gifImage, string gifPath)
 {
     FileStream fs = null;
     try
     {
         fs = new FileStream(gifPath, FileMode.Create);
         StreamHelper streamHelper = new StreamHelper(fs);
         streamHelper.WriteHeader(gifImage.Header);
         streamHelper.WriteLSD(gifImage.LogicalScreenDescriptor);
         if (gifImage.LogicalScreenDescriptor.GlobalColorTableFlag)
         {
             streamHelper.SetGlobalColorTable(gifImage.GlobalColorTable);
         }
         streamHelper.SetApplicationExtensions(gifImage.ApplictionExtensions);
         streamHelper.SetCommentExtensions(gifImage.CommentExtensions);
         SetFrames(gifImage.Frames, streamHelper, fs);
     }
     catch
     {
         throw;
     }
     finally
     {
         if (fs != null)
         {
             fs.Close();
         }
     }
 }
예제 #3
0
        static void SetFrames(List<GifFrame> frames,StreamHelper streamHelper,Stream fs)
        {
            foreach (GifFrame f in frames)
            {
                List<byte> list = new List<byte>();
                if (f.GraphicExtension != null)
                {
                    list.AddRange(f.GraphicExtension.GetBuffer());
                }
                f.ImageDescriptor.SortFlag = false;
                f.ImageDescriptor.InterlaceFlag = false;
                list.AddRange(f.ImageDescriptor.GetBuffer());
                if (f.ImageDescriptor.LctFlag)
                {
                    list.AddRange(f.LocalColorTable);
                }
                streamHelper.WriteBytes(list.ToArray());
                int transIndex = -1;

                if (f.GraphicExtension.TransparencyFlag)
                {
                    transIndex = f.GraphicExtension.TranIndex;
                }

                byte[] indexedPixel = GetImagePixels(f.Image, f.LocalColorTable, transIndex);

                LZWEncoder lzw = new LZWEncoder(indexedPixel, (byte)f.ColorDepth);
                lzw.Encode(fs);
                streamHelper.WriteBytes(new byte[] { 0 });
            }
            streamHelper.WriteBytes(new byte[] { 0x3B });
        }
예제 #4
0
        internal static GifImage Decode(Stream stream)
        {
            StreamHelper     streamHelper = null;
            GifImage         gifImage     = new GifImage();
            List <GraphicEx> graphics     = new List <GraphicEx>();
            int frameCount = 0;

            try
            {
                streamHelper = new StreamHelper(stream);
                //读取文件头
                gifImage.Header = streamHelper.ReadString(6);
                //读取逻辑屏幕标示符
                gifImage.LogicalScreenDescriptor = streamHelper.GetLCD(stream);
                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, stream, gifImage, graphics, frameCount);
                        frameCount++;
                    }
                    else if (nextFlag == GifExtensions.ExtensionIntroducer)
                    {
                        int gcl = streamHelper.Read();
                        switch (gcl)
                        {
                        case GifExtensions.GraphicControlLabel:
                        {
                            GraphicEx graphicEx = streamHelper.GetGraphicControlExtension(stream);
                            graphics.Add(graphicEx);
                            break;
                        }

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

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

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

                gifImage.Texture = new Texture2D(gifImage.Width, gifImage.Height, TextureFormat.ARGB32, false);
            }
            catch
            {
                throw;
            }
            finally
            {
                stream.Close();
            }
            return(gifImage);
        }
예제 #5
0
 /// <summary>
 /// ��gifͼ���ļ����н���
 /// </summary>
 /// <param name="gifPath">gif�ļ�·��</param>
 internal static GifImage Decode(string gifPath)
 {
     FileStream fs=null;
     StreamHelper streamHelper=null;
     GifImage gifImage = new GifImage();
     List<GraphicEx> graphics = new List<GraphicEx>();
     int frameCount = 0;
     try
     {
         fs = new FileStream(gifPath,FileMode.Open);
         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();
         }
     }
     catch
     {
         throw;
     }
     finally
     {
         fs.Close();
     }
     return gifImage;
 }
예제 #6
0
 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[] piexel = lzwDecoder.DecodeImageData(imgDes.Width, imgDes.Height, dataSize);
     frame.IndexedPixel = piexel;
     int blockSize = streamHelper.Read();
     DataStruct data = new DataStruct(blockSize, fs);
     GraphicEx graphicEx = graphics[frameCount];
     frame.GraphicExtension = graphicEx;
     Bitmap img = GetImageFromPixel(piexel, frame.Palette, imgDes.InterlaceFlag, imgDes.Width, imgDes.Height);
     frame.Image = img;
     gifImage.Frames.Add(frame);
 }
예제 #7
0
        /// <summary>
        /// 对gif图像文件进行解码
        /// </summary>
        /// <param name="gifPath">gif文件路径</param>
        public static GifImage Decode(string gifPath)
        {
            FileStream       fs           = null;
            StreamHelper     streamHelper = null;
            GifImage         gifImage     = new GifImage();
            List <GraphicEx> graphics     = new List <GraphicEx>();
            int frameCount = 0;

//            try
//            {
            fs           = new FileStream(gifPath, FileMode.Open);
            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();
            }
//            }
//            catch
//            {
//                throw;
//            }
//            finally
//            {
            fs.Close();
//            }
            return(gifImage);
        }
예제 #8
0
 internal CommentEx GetCommentEx(Stream stream)
 {
     CommentEx cmtEx = new CommentEx();
     StreamHelper streamHelper = new StreamHelper(stream);
     cmtEx.CommentDatas = new List<string>();
     int nextFlag = streamHelper.Read();
     cmtEx.CommentDatas = new List<string>();
     while (nextFlag != 0)
     {
         int blockSize = nextFlag;
         string data = streamHelper.ReadString(blockSize);
         cmtEx.CommentDatas.Add(data);
         nextFlag = streamHelper.Read();
     }
     return cmtEx;
 }