コード例 #1
0
        public LZW(IEncoder encoder)
        {
            this.encoder = encoder;
            this.root    = new LZWNode(0, 0);
            this.buffer  = new Stack <byte>();
            allNodes     = new Dictionary <long, LZWNode>();

            for (int i = 0; i <= 255; i++)
            {
                LZWNode child = new LZWNode((byte)i, i);
                root.AddChild(child);
                allNodes[i] = child;
            }
            currentNode = this.root;
        }
コード例 #2
0
        private void DecodeNewValue()
        {
            Stack <byte> traceBuffer = new Stack <byte>();

            valueToDecode = encoder.Decode();
            if (!allNodes.ContainsKey(valueToDecode))
            {
                LZWNode backTraceNode = currentNode;
                while (backTraceNode.parent != root)
                {
                    backTraceNode = backTraceNode.parent;
                }
                LZWNode child = new LZWNode(backTraceNode.value, valueToDecode);
                currentNode.AddChild(child);
                currentNode.AddUsage();
                currentNode             = child;
                allNodes[valueToDecode] = child;
            }
            LZWNode traceNode  = allNodes[valueToDecode];
            byte    lastValue  = traceNode.value;
            byte    firstValue = 0;

            while (traceNode.parent != null)
            {
                buffer.Push(traceNode.value);
                traceBuffer.Push(traceNode.value);
                firstValue = traceNode.value;
                traceNode  = traceNode.parent;
            }
            if (!currentNode.ContainsValue(firstValue))
            {
                LZWNode child = new LZWNode(firstValue, nextNodeValue);
                currentNode.AddChild(child);
                allNodes[nextNodeValue] = child;
                currentNode             = allNodes[valueToDecode];
                IncreaseNextNodeValue();
            }
            else
            {
                currentNode = currentNode.GetChild(firstValue);
            }
            encoder.AddValue(nextNodeValue);
            if (buffer.Count() > maxBufferLength)
            {
                maxBufferLength = buffer.Count();
                maxBufferNode   = allNodes[valueToDecode];
            }
        }
コード例 #3
0
        public void Encode(byte value)
        {
            if (currentNode.ContainsValue(value))
            {
                currentNode = currentNode.GetChild(value);
            }
            else
            {
                LZWNode child = new LZWNode(value, nextNodeValue);
                currentNode.AddChild(child);
                allNodes[nextNodeValue] = child;
                encoder.Encode(currentNode.identifier);
                currentNode.AddUsage();
                encoder.AddValue(nextNodeValue);
                IncreaseNextNodeValue();

                currentNode = root.GetChild(value);
            }
        }
コード例 #4
0
 public void AddChild(LZWNode child)
 {
     child.parent = this;
     this.children[child.value] = child;
 }