⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lzw_encode.cs

📁 PDF文件格式解析库源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using QiHe.CodeLib.Compress.LempelZivWelch;

namespace QiHe.CodeLib.Compress
{
    public partial class Lzw
    {
        const int ClearTableMarker = 256;
        const int EOD = 257;

        public static void Encode(Stream input, Stream output)
        {
            MemoryStream memStream = new MemoryStream();
            BitStream outStream = new BitStream(memStream);
            // <code, numbits>
            foreach (Pair<int, int> code in Analyze(input))
            {
                outStream.WriteBitsBigEndian(code.Left, code.Right);
            }
            outStream.Flush();
            memStream.Position = 0;
            BitOrder.Reverse(memStream, output);
        }

        private static void ClearTable(Dictionary<TableEntry, int> Table)
        {
            Table.Clear();
            for (int i = 0; i <= 257; i++)
            {
                Table.Add(new TableEntry(i), i);
            }
        }

        private static IEnumerable<Pair<int, int>> Analyze(Stream input)
        {
            Dictionary<TableEntry, int> Table = new Dictionary<TableEntry, int>();
            ClearTable(Table);
            int numbits = 9;
            yield return new Pair<int, int>(ClearTableMarker, numbits);

            TableEntry word = new TableEntry(-1);
            TableEntry phrase = new TableEntry();
            while (true)
            {
                int symbol = input.ReadByte();

                int wordIndex = Table.ContainsKey(word) ? Table[word] : -1;

                if (symbol == -1)
                {
                    yield return new Pair<int, int>(wordIndex, numbits);
                    break;
                }

                phrase.RefIndex = wordIndex;
                phrase.Symbol = symbol;

                if (Table.ContainsKey(phrase))
                {
                    word = phrase;
                }
                else
                {
                    yield return new Pair<int, int>(wordIndex, numbits);
                    if (Table.Count < 4096)
                    {
                        Table.Add(phrase, Table.Count);
                        if (numbits == 9 && Table.Count > 511)
                        {
                            numbits = 10;
                        }
                        else if (numbits == 10 && Table.Count > 1023)
                        {
                            numbits = 11;
                        }
                        else if (numbits == 11 && Table.Count > 2047)
                        {
                            numbits = 12;
                        }
                    }
                    else
                    {
                        ClearTable(Table);
                        yield return new Pair<int, int>(ClearTableMarker, numbits);
                        numbits = 9;
                    }
                    word.Symbol = symbol;
                    word.RefIndex = -1;
                }
            }
            yield return new Pair<int, int>(EOD, numbits);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -