• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            飯中淹的避難所~~~~~

            偶爾來避難的地方~

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              94 隨筆 :: 0 文章 :: 257 評論 :: 0 Trackbacks
            #pragma once

            #include 
            "xStream.h"

            namespace xlibplus
            {
                
            namespace crypt{
                    template 
            <bool fValue, typename TThen, typename TElse>
                    
            struct if_then_else
                    {
                        typedef TThen result;
                    };

                    template 
            <typename TThen, typename TElse>
                    
            struct if_then_else<false, TThen, TElse>
                    {
                        typedef TElse result;
                    };

                    template 
            <int nBits>
                    
            struct UnsignedTypeSelector
                    {
                        typedef typename if_then_else
            <(nBits<=8), unsigned char, typename if_then_else<(nBits<=16), unsigned short, unsigned int>::result >::result Type;
                    };

                    template 
            <int nMaxCodeBits = 12>
                    
            class xLzwDict
                    {
                        typedef typename UnsignedTypeSelector
            <nMaxCodeBits>::Type TCode;
                        typedef 
            struct tagEntry
                        {
                            TCode nPrefix;
                            TCode nPostfix;
                            TCode nFirstChild;
                            TCode nNext;
                        }t_Entry;
                    
            public:
                        xLzwDict( 
            int nCharBits ) : m_nCodeStartIndex( (1<<nCharBits)+2 ), m_nClearCode( (1<<nCharBits) ), m_nEndCode( (1<<nCharBits)+1 ), m_nCharBits(nCharBits)
                        {
                            Reset();
                        }

                        
            ~xLzwDict()
                        {
                        }

                        
            bool IsDefinedCode( TCode nValue ) const
                        {
                            
            return (IsCode( nValue ) && m_Entries[nValue].nPostfix != m_nEndCode );
                        }

                        
            bool IsCode( TCode nValue ) const
                        {
                            
            return (nValue >= m_nCodeStartIndex);
                        }

                        TCode GetFirstChar( TCode nCode ) 
            const
                        {
                            
            while( IsCode( nCode ) )
                                nCode 
            = m_Entries[nCode].nPrefix;
                            
            return nCode;
                        }

                        TCode Search( TCode nPrefix, TCode nPostfix ) 
            const
                        {
                            t_Entry 
            & top = m_Entries[nPrefix];

                            TCode nCode 
            = top.nFirstChild;

                            
            while( nCode != 0 )
                            {
                                
            if( m_Entries[nCode].nPostfix == nPostfix )return nCode;
                                nCode 
            = m_Entries[nCode].nNext;
                            }

                            
            return 0;
                        }

                        TCode Add( TCode nPrefix, TCode nPostfix )
                        {
                            
            if( m_nCodeIndex >= (1<<nMaxCodeBits) )return 0;

                            t_Entry 
            & top = m_Entries[nPrefix];
                            t_Entry 
            & cur = m_Entries[m_nCodeIndex];

                            cur.nPrefix 
            = nPrefix;
                            cur.nPostfix 
            = nPostfix;
                            cur.nNext 
            = top.nFirstChild;
                            cur.nFirstChild 
            = 0;
                            top.nFirstChild 
            = m_nCodeIndex;

                            TCode nCode 
            = m_nCodeIndex;

                            
            ++m_nCodeIndex;

                            
            if( m_nCodeIndex >= (1 << m_nCodeBits) && m_nCodeBits < nMaxCodeBits ) ++ m_nCodeBits;

                            
            return nCode;
                        }

                        
            void Reset()
                        {
                            m_nCodeIndex 
            = m_nCodeStartIndex;

                            m_nCodeBits 
            = _CalcCodeBits( m_nCodeIndex );

                            UINT nCharCount 
            = (1<<m_nCharBits);

                            
            for( UINT n = 0;n < nCharCount;n ++ )
                            {
                                m_Entries[n].nPrefix 
            = n;
                                m_Entries[n].nPostfix 
            = 0;
                                m_Entries[n].nNext 
            = 0;
                                m_Entries[n].nFirstChild 
            = 0;
                            }

                            
            for( UINT n = nCharCount;n < (1<<nMaxCodeBits); ++n )
                            {
                                m_Entries[n].nPrefix 
            = m_nEndCode;
                                m_Entries[n].nPostfix 
            = m_nEndCode;
                                m_Entries[n].nFirstChild 
            = 0;
                                m_Entries[n].nNext 
            = 0;
                            }
                        }

                        TCode GetPrefix( TCode nCode ) 
            const { 
                            
            if( nCode >= m_nCodeIndex )return 0;
                            
            return m_Entries[nCode].nPrefix;
                        }

                        TCode GetPostfix( TCode nCode ) 
            const {
                            
            if( nCode >= m_nCodeIndex )return 0;
                            
            return m_Entries[nCode].nPostfix;
                        }

                        
            int GetCodeBits() const { return m_nCodeBits;}
                        TCode GetClearCode() 
            const { return m_nClearCode;}
                        TCode GetEndCode() 
            const { return m_nEndCode;}
                        TCode GetCodeIndex() 
            const { return m_nCodeIndex;}
                    
            protected:
                        
            int _CalcCodeBits( TCode nCode )
                        {
                            
            forint i = 1;i <= nMaxCodeBits;i ++ )
                            {
                                
            if( nCode < (1<<i) )return i;
                            }
                            
            return 1;
                        }


                        t_Entry m_Entries[(
            1<<nMaxCodeBits)];

                        TCode m_nCodeIndex;
                        
            const TCode m_nCodeStartIndex;
                        
            const TCode m_nClearCode;
                        
            const TCode m_nEndCode;
                        
            int m_nCodeBits;
                        
            const int m_nCharBits;
                    };


                    template 
            <int nMaxCodeBits, bool fDynamicBits = true>
                    
            class xLzw
                    {
                        typedef xLzwDict
            <nMaxCodeBits> TDict;
                        typedef typename UnsignedTypeSelector
            <nMaxCodeBits>::Type TCode;

                        template 
            <bool fDynamic>
                        inline 
            int _GetCodeBits(TDict & dict) const 
                        {
                            
            return dict.GetCodeBits();
                        }
                        template 
            <>
                        inline 
            int _GetCodeBits<false>(TDict & dict) const 
                        {
                            
            return nMaxCodeBits;
                        }
                    
            public:
                        xLzw( 
            int nCharBits ) : m_nCharBits( nCharBits ) {}
                        
            ~xLzw() {}

                        
            void Encode( IIStream * is, IOStream * os, TDict & dict )
                        {
                            TCode nPrefix;
                            TCode nPostfix;
                            TCode nCode;

                            
            ifis->Eof() )return;

                            xIBitStream bis( 
            is );
                            xOBitStream bos( os );

                            bis.Read( 
            &nPrefix, m_nCharBits );

                            
            while!bis.Eof() )
                            {
                                bis.Read( 
            &nPostfix, m_nCharBits );
                                
                                nCode 
            = dict.Search( nPrefix, nPostfix );

                                
            if( nCode == 0 )
                                {
                                    bos.Write( nPrefix, _GetCodeBits
            <fDynamicBits>(dict) );
                                    nCode 
            = dict.Add( nPrefix, nPostfix );
                                    
            if( nCode == 0 )
                                    {
                                        bos.Write( dict.GetClearCode(), _GetCodeBits
            <fDynamicBits>(dict) );
                                        dict.Reset();
                                    }
                                    nPrefix 
            = nPostfix;
                                }
                                
            else
                                {
                                    nPrefix 
            = nCode;
                                }
                            }

                            bos.Write( nPrefix, _GetCodeBits
            <fDynamicBits>(dict) );
                            bos.Flush();
                        }

                        
            void OutputCode( TCode nCode, xOBitStream & os, TDict & dict )
                        {
                            
            if( nCode > dict.GetEndCode() )
                            {
                                OutputCode( dict.GetPrefix( nCode ), os, dict );
                                OutputCode( dict.GetPostfix( nCode ), os, dict );
                            }
                            
            else
                                os.Write( nCode, m_nCharBits );
                        }

                        
            void Decode( IIStream * is, IOStream * os, TDict & dict )
                        {
                            TCode nCode 
            = 0;
                            TCode nOldCode 
            = dict.GetEndCode();
                            TCode nResult 
            = 0;
                            TCode nClearCode 
            = dict.GetClearCode();
                            TCode nEndCode 
            = dict.GetEndCode();

                            xIBitStream bis( 
            is );
                            xOBitStream bos( os );

                            
            while!bis.Eof() )
                            {
                                bis.Read( 
            &nCode, _GetCodeBits<fDynamicBits>(dict) );

                                
            if( nCode == nEndCode )
                                    
            break;
                                
            else if( nCode == nClearCode )
                                {
                                    dict.Reset();
                                    nOldCode 
            = nEndCode;
                                    
            continue;
                                }
                                
            else
                                {
                                    
            if( nCode < dict.GetCodeIndex() )
                                    {
                                        OutputCode( nCode, bos, dict );
                                        
            if( nOldCode != nEndCode )
                                            nResult 
            = dict.Add( nOldCode, dict.GetFirstChar( nCode ) );
                                    }
                                    
            else if( nCode > dict.GetCodeIndex() )
                                    {
                                        
            return;
                                    }
                                    
            else
                                    {
                                        TCode nFirstChar 
            = dict.GetFirstChar( nOldCode );
                                        OutputCode( nOldCode, bos, dict );
                                        OutputCode( nFirstChar, bos, dict );
                                        nResult 
            = dict.Add( nOldCode, dict.GetFirstChar( nOldCode ) );
                                    }
                                }
                                nOldCode 
            = nCode;
                            }

                            bos.Flush();
                        }
                    
            private:
                        
            const int m_nCharBits;
                    };
                };
            };
            posted on 2009-01-09 09:15 飯中淹 閱讀(1537) 評論(1)  編輯 收藏 引用

            評論

            # re: xLibPlus專題(2) - LZW算法 可用于GIF編解碼 2009-01-11 23:55 silvasaga
            頭兒, 我來支持你下
            這是你最近寫的嗎?  回復  更多評論
              

            国产精品伊人久久伊人电影 | 久久精品成人| 久久99国产一区二区三区| 亚洲成av人片不卡无码久久| 久久久精品久久久久影院| 久久综合给合久久狠狠狠97色69 | 久久久久国产精品| 久久国产V一级毛多内射| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久精品国产一区二区电影| 久久精品国产亚洲αv忘忧草| 99久久婷婷国产综合亚洲| 久久黄视频| 精品国产福利久久久| 久久丫忘忧草产品| 国内精品久久久久久久久电影网 | 久久91精品国产91久| 国产精品久久久久9999| 久久亚洲精品人成综合网| 国内精品久久久久久久coent| 蜜臀av性久久久久蜜臀aⅴ| 伊人久久大香线蕉无码麻豆 | 国产真实乱对白精彩久久| 久久精品国产99久久无毒不卡 | 国产美女亚洲精品久久久综合 | 久久久久久国产精品美女| 国产精品美女久久久久网| 伊人久久久AV老熟妇色| 亚洲伊人久久成综合人影院| 国内精品伊人久久久久网站| 国产精品久久久久久搜索| 久久婷婷五月综合97色| 久久中文字幕人妻熟av女| 欧美久久亚洲精品| 99久久精品免费观看国产| www.久久热| 久久综合狠狠色综合伊人| 国产91久久综合| 精品久久久久久无码国产| 久久精品国产精品亚洲艾草网美妙| 国产福利电影一区二区三区,免费久久久久久久精 |