예제 #1
0
        public void Combinations_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

            byte runSizeInput3 = 2;
            ushort codeWordInput3 = 4; // 100 in base 2
            byte lengthInput3 = 8; // 1000 in base 2

            HuffmanElement huffmanTestElement1 = new HuffmanElement(runSizeInput1, codeWordInput1, lengthInput1);
            HuffmanElement huffmanTestElement2 = new HuffmanElement(runSizeInput2, codeWordInput2, lengthInput2);
            HuffmanElement huffmanTestElement3 = new HuffmanElement(runSizeInput3, codeWordInput3, lengthInput3);
            HuffmanTable huffTable1 = new HuffmanTable(huffmanTestElement1, huffmanTestElement2, huffmanTestElement3);

            byte[] numberOfCodesOutput = huffTable1.Combinations();

            Assert.AreEqual(3, numberOfCodesOutput[7]);
        }
예제 #2
0
        private void _writeHuffmanSegment(HuffmanTable huffman, byte id, bool dc) {
            _jw.WriteBytes(0xff, 0xc4); //DHT marker

            ushort len = (ushort)(huffman.Elements.Count + huffman.Combinations().Length + 3);
            _jw.WriteBytes((byte)(len >> 8), (byte)(len & 0xff));

            byte combined;
            if (!dc) {
                combined = (byte)((1 << 4) + id);
            } else {
                combined = id;
            }

            _jw.WriteBytes(combined);

            _jw.WriteBytes(huffman.Combinations());


            HuffmanElement[] allElements = huffman.Elements.Values.OrderBy(x => x.Length).ThenBy(x => x.RunSize).ToArray();
            foreach (HuffmanElement huffmanElement in allElements) {
                _jw.WriteBytes(huffmanElement.RunSize);
            }
        }
예제 #3
0
        private void _writeHuffmanSegment(HuffmanTable huffman, byte id, bool dc) {
            //DHT marker
            _jw.WriteBytes(0xff, 0xc4);

            //Size of Huffman table in bytes. Calculated as the number of elements +
            //the number of combinations + three bytes. Two of them for the length 
            //itself and the last is for whether it is a DC or not and the ID.
            ushort len = (ushort)(huffman.Elements.Count + huffman.Combinations().Length + 3);
            _jw.WriteBytes((byte)(len >> 8), (byte)(len & 0xff));

            byte combined;
            if (!dc) {
                combined = (byte)((1 << 4) + id);
            } else {
                combined = id;
            }

            //DC/AC and ID
            _jw.WriteBytes(combined);

            //The table combinations
            _jw.WriteBytes(huffman.Combinations());

            //Write the elements
            HuffmanElement[] allElements = huffman.Elements.Values.OrderBy(x => x.Length).ThenBy(x => x.RunSize).ToArray();
            foreach (HuffmanElement huffmanElement in allElements) {
                _jw.WriteBytes(huffmanElement.RunSize);
            }
        }