Пример #1
0
        // tree traversal
        public void StoreCodes(HuffmanTreeNode root, string s)
        {
            // base case; if the left and right are null then its a leaf node and store the code s generated by traversing the tree.
            if (root.Left == null && root.Right == null && Char.IsLetter(root.Char))
            {
                // codetable char
                if (!CodeTable.ContainsKey(root.Char))
                {
                    CodeTable.Add(root.Char, s);
                }
                CodeTable[root.Char] = s;
                Console.WriteLine("\t\t" + root.Char + ":" + s);
                return;
            }

            // if we go to left then add "0" to the code., if we go to the right add "1" to the code.
            // recursive calls for left and right sub-tree of the generated tree.
            StoreCodes(root.Left, s + "0");
            StoreCodes(root.Right, s + "1");
        }
Пример #2
0
        public List <int> Decompress(string imageData, int startCodeLength, int colorTableSize)
        {
            var clearCode        = colorTableSize;
            var endOfInformation = colorTableSize + 1;

            ImageData = imageData;
            InitializeCodeTable(colorTableSize);
            var result     = new List <int>();
            var codeLength = startCodeLength;

            //first code from 0 to codeLength is always control code - CC, ignore it
            var pos  = codeLength;
            var code = Convert.ToInt32(Reverse(ImageData.Substring(pos, codeLength)), 2);

            result.AddRange(CodeTable[code]);
            pos += codeLength;

            var nextCode = 0;
            var codeLengthIncreaseThreshold = 1 << codeLength;

            while (endOfInformation != nextCode)
            {
                if (nextCode == clearCode)
                {
                    InitializeCodeTable(colorTableSize);
                    //skip CC control code
                    pos       += codeLength;
                    codeLength = startCodeLength;
                    codeLengthIncreaseThreshold = 1 << codeLength;
                    code = Convert.ToInt32(Reverse(ImageData.Substring(pos, codeLength)), 2);
                    result.AddRange(CodeTable[code]);
                    pos += codeLength;
                }

                var prevCode = code;

                if (CodeTable.Count == codeLengthIncreaseThreshold)
                {
                    codeLength++;
                    codeLengthIncreaseThreshold = 1 << codeLength;
                }
                // if (codeLength == 13)
                // {
                //     codeLength = 12;
                //
                // }
                code     = Convert.ToInt32(Reverse(ImageData.Substring(pos, codeLength)), 2);
                pos     += codeLength;
                nextCode = Convert.ToInt32(Reverse(ImageData.Substring(pos, codeLength)), 2);

                if (CodeTable.ContainsKey(code))
                {
                    result.AddRange(CodeTable[code]);
                    var k = CodeTable[code][0];
                    CodeTable[CodeTable.Count] = CodeTable[prevCode].Concat(new List <int> {
                        k
                    }).ToList();
                }
                else
                {
                    var prevCodeValue = CodeTable[prevCode];
                    var k             = prevCodeValue[0];
                    result.AddRange(prevCodeValue.Concat(new List <int> {
                        k
                    }).ToList());
                    CodeTable[CodeTable.Count] = prevCodeValue.Concat(new List <int> {
                        k
                    }).ToList();
                }
            }
            return(result);
        }