Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / C#

Project Tool

Rate me:
Please Sign up or sign in to vote.
4.69/5 (10 votes)
23 Sep 2007CPOL3 min read 54.6K   1.7K   73  
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
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);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions