예제 #1
0
        public void HasCode_Test()
        {
            byte runSizeInput1 = 00000000;
            ushort codeWordInput1 = 2; // 10 in base 2
            byte lengthInput1 = 8; // 1000 in base 2

            byte runSizeInput2 = 1;
            ushort codeWordInput2 = 3; // 11 in base 2
            byte lengthInput2 = 8; // 1000 in base 2

            HuffmanElement huffmanTestElement1 = new HuffmanElement(runSizeInput1, codeWordInput1, lengthInput1);
            HuffmanElement huffmanTestElement2 = new HuffmanElement(runSizeInput2, codeWordInput2, lengthInput2);

            HuffmanTable huffTable1 = new HuffmanTable(huffmanTestElement1, huffmanTestElement2);

            Assert.AreEqual(0, huffTable1.HasCode(2, 8).RunSize);
        }
예제 #2
0
        public void HasCodeTest_for_null_when_no_element_has_code()
        {
            byte runSizeInput1 = 00000000;
            ushort codeWordInput1 = 2; // 10 in base 2
            byte lengthInput1 = 8; // 1000 in base 2

            byte runSizeInput2 = 1;
            ushort codeWordInput2 = 3; // 11 in base 2
            byte lengthInput2 = 8; // 1000 in base 2

            HuffmanElement huffmanTestElement1 = new HuffmanElement(runSizeInput1, codeWordInput1, lengthInput1);
            HuffmanElement huffmanTestElement2 = new HuffmanElement(runSizeInput2, codeWordInput2, lengthInput2);

            HuffmanTable huffTable1 = new HuffmanTable(huffmanTestElement1, huffmanTestElement2);

            Assert.AreEqual(null, huffTable1.HasCode(1, 8));
        }
예제 #3
0
        // Gets the runsize and returns the amount of read zeroes and the decoded huffman value
        private int nextValue(BitList bits, ref int index, HuffmanTable huffmanTable, out int zeroes) {
            HuffmanElement e = null;
            int i;
            ushort code = 0;
            for (i = 1; i <= 16; i++) {
                code <<= 1;
                code += (ushort)(bits[index] ? 1 : 0);
                index++;
                e = huffmanTable.HasCode(code, i);
                if (e != null) {
                    break;
                }
            }
            if (e == null) {
                throw new ArgumentNullException();
            }

            zeroes = (e.RunSize & 0xF0) >> 4;
            int category = e.RunSize & 0xF;

            ushort value = 0;
            for (i = 0; i < category; i++) {
                value <<= 1;
                value += (ushort)(bits[index] ? 1 : 0);
                index++;
            }

            return lookupValue(value, category);
        }