private void InitialiseEncodingTable() { #region Patterns var patterns = new List <int> { 0x6cc, 0x66c, 0x666, 0x498, 0x48c, 0x44c, 0x4c8, 0x4c4, 0x464, 0x648, 0x644, 0x624, 0x59c, 0x4dc, 0x4ce, 0x5cc, 0x4ec, 0x4e6, 0x672, 0x65c, 0x64e, 0x6e4, 0x674, 0x76e, 0x74c, 0x72c, 0x726, 0x764, 0x734, 0x732, 0x6d8, 0x6c6, 0x636, 0x518, 0x458, 0x446, 0x588, 0x468, 0x462, 0x688, 0x628, 0x622, 0x5b8, 0x58e, 0x46e, 0x5d8, 0x5c6, 0x476, 0x776, 0x68e, 0x62e, 0x6e8, 0x6e2, 0x6ee, 0x758, 0x746, 0x716, 0x768, 0x762, 0x71a, 0x77a, 0x642, 0x78a, 0x530, 0x50c, 0x4b0, 0x486, 0x42c, 0x426, 0x590, 0x584, 0x4d0, 0x4c2, 0x434, 0x432, 0x612, 0x650, 0x7ba, 0x614, 0x47a, 0x53c, 0x4bc, 0x49e, 0x5e4, 0x4f4, 0x4f2, 0x7a4, 0x794, 0x792, 0x6de, 0x6f6, 0x7b6, 0x578, 0x51e, 0x45e, 0x5e8, 0x5e2, 0x7a8, 0x7a2, 0x5de, 0x5ee, 0x75e, 0x7ae }; #endregion EncodingTable = new EncodingTable(); EncodingTable.AddRange(patterns.Select(p => new CodeWord(p))); // Add StartCodes EncodingTable.Add(new StartWordA()); EncodingTable.Add(new StartWordB()); EncodingTable.Add(new StartWordC()); }
/// <summary> /// Compresses the file stored in the <paramref name="path" /> and stores compressed file it the /// <paramref name="filePart" />. /// </summary> /// <param name="path">The path to the file to be compressed.</param> /// <param name="filePart">The archive part to store the compressed file in.</param> /// <param name="frequencyTable">The frequency table to be saved to the archive.</param> private void CompressFile(string path, PackagePart filePart, out FrequencyTable frequencyTable) { /* open the file */ var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize); /* find frequencies of bytes in the file */ OnStateChange("counting up frequencies of bytes"); frequencyTable = new FrequencyTable(fileStream); frequencyTable.SubscribeToUpdates(OnProgressChange); frequencyTable.CountFrequencies(); /* rewind the position of the stream to use this stream once again */ fileStream.Position = 0; /* build the Huffman tree based on the counted frequencies */ OnStateChange("building the Huffman tree"); var huffmanTree = new HuffmanTree(frequencyTable); /* get the Huffman code from the tree */ OnStateChange("retrieving the Huffman code"); var encodingTable = new EncodingTable(huffmanTree); /* get the file part's stream */ var filePartStream = new BufferedStream(filePart.GetStream(), BufferSize); /* encode the file */ OnStateChange("compressing the file"); var encoder = new Encoder(fileStream, filePartStream, encodingTable); encoder.SubscribeToUpdates(OnProgressChange); encoder.Encode(); /* dispose unused streams */ fileStream.Close(); filePartStream.Close(); }
public static byte[] GetBytes(string s) { byte[] returnData = new byte[s.Length]; for (int i = 0; i < s.Length; i++) { returnData[i] = EncodingTable.FirstOrDefault(et => et.Character == s[i].ToString()).Index; } return(returnData); }
public void Dispose() { EncodingTable.Clear(); EncodingTable = null; TextMatrices[0].Clear(); TextMatrices[1].Clear(); TextMatrices[2].Clear(); TextMatrices[0] = null; TextMatrices[1] = null; TextMatrices[2] = null; }
public void EncodingTest3() { GenerateByteStream("EncodingTest3", 15, 16, false); var fileStream = new FileStream("EncodingTest3", FileMode.Open, FileAccess.Read); var bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, (int)fileStream.Length); var frequencyTable = new FrequencyTable(bytes); var huffmanTree = new HuffmanTree(frequencyTable); var encodingTable = new EncodingTable(huffmanTree); Console.WriteLine(encodingTable.ToString()); }
Dictionary <char, int> ParseCmapTable(Stream stream, BinaryReader reader, TableHeader header) { stream.Position = header.Offset + 2; int tableCount = reader.ReadUInt16(); var tableList = new List <EncodingTable>(); for (int i = 0; i < tableCount; i++) { var table = new EncodingTable() { PlatformId = reader.ReadUInt16(), EncodingId = reader.ReadUInt16(), Offset = reader.ReadUInt32(), }; tableList.Add(table); } EncodingTable unicodeTable = tableList.Find(table => table.PlatformId == 3 && table.EncodingId == 1); stream.Position = header.Offset + unicodeTable.Offset + 2; int length = reader.ReadUInt16(); stream.Position += 2; int segCount = reader.ReadUInt16() / 2; stream.Position += 6; var endCode = new int[segCount]; for (int i = 0; i < segCount; i++) { endCode[i] = reader.ReadUInt16(); } stream.Position += 2; var startCode = new int[segCount]; for (int i = 0; i < segCount; i++) { startCode[i] = reader.ReadUInt16(); } var idDelta = new int[segCount]; for (int i = 0; i < segCount; i++) { idDelta[i] = reader.ReadInt16(); } long offset = stream.Position; var idRangeOffset = new int[segCount]; for (int i = 0; i < segCount; i++) { idRangeOffset[i] = reader.ReadUInt16(); } var glyphDictionary = new Dictionary <char, int>(); for (int i = 0; i < segCount - 1; i++) { if (idRangeOffset[i] == 0) { for (int c = startCode[i]; c <= endCode[i]; c++) { glyphDictionary.Add((char)c, idDelta[i] + c); } } else { for (int c = startCode[i]; c <= endCode[i]; c++) { stream.Position = idRangeOffset[i] + 2 * (c - startCode[i]) + offset + i * 2; glyphDictionary.Add((char)c, idDelta[i] + reader.ReadUInt16()); } } } return(glyphDictionary); }
public static string GetString(byte[] bytes) => bytes.TakeWhile(b => b != TerminationCharacter).Aggregate(string.Empty, (current, b) => current + EncodingTable.FirstOrDefault(et => et.Index == b).Character);
public DecodingTable(HuffmanTree tree) { var encodingTable = new EncodingTable(tree); SetCodes(encodingTable); }
/// <summary> /// Initializes an encoder that encodes the <paramref name="inputStream" /> bytes with codes in the /// <paramref name="encodingTable" /> to the <paramref name="outputStream" />. /// </summary> /// <param name="inputStream">The stream to read bytes from.</param> /// <param name="outputStream">The stream to write encoded bytes to.</param> /// <param name="encodingTable">The table containing Huffman codes for bytes.</param> public Encoder(Stream inputStream, Stream outputStream, EncodingTable encodingTable) { InputStream = inputStream; OutputStream = outputStream; EncodingTable = encodingTable; }