/** * Write out the selector list and Huffman tables * @Exception on any I/O error writing the data */ private void writeSelectorsAndHuffmanTables() { int totalSelectors = selectors.Length; int totalTables = huffmanCodeLengths.GetLength(0); bitOutputStream.WriteBits (3, (uint)totalTables); bitOutputStream.WriteBits (15, (uint)totalSelectors); // Write the selectors var selectorMTF = new MoveToFront(); for (int i = 0; i < totalSelectors; i++) { bitOutputStream.WriteUnary (selectorMTF.ValueToFront (selectors[i])); } // Write the Huffman tables for (int i = 0; i < totalTables; i++) { var currentLength = huffmanCodeLengths[i,0]; bitOutputStream.WriteBits (5, (uint)currentLength); for (var j = 0; j < mtfAlphabetSize; j++) { var codeLength = huffmanCodeLengths[i,j]; var value = (currentLength < codeLength) ? 2u : 3u; var delta = Math.Abs (codeLength - currentLength); while (delta-- > 0) { bitOutputStream.WriteBits (2, value); } bitOutputStream.WriteBoolean (false); currentLength = codeLength; } } }