private void StoreBttn_Click(object sender, EventArgs e) { bread = new BitReader(filePath.FullName); bmpHeader = new List <uint>(readBMPHeader()); bread.Dispose(); string outputName = filePath.Name + "[" + imgPred.predictionNumber + "].pre"; bwrite = new BitWriter(Path.Combine(filePath.DirectoryName, outputName)); foreach (var item in bmpHeader) { bwrite.WriteNBits(8, item); } bwrite.WriteNBits(4, (uint)imgPred.predictionNumber); for (int i = 0; i < imgLength; i++) { for (int j = 0; j < imgLength; j++) { if (imgPred.errorMatrix[i, j] < 0) { bwrite.WriteNBits(1, 1); } else if (imgPred.errorMatrix[i, j] >= 0) { bwrite.WriteNBits(1, 0); } bwrite.WriteNBits(8, (uint)Math.Abs(imgPred.errorMatrix[i, j])); } } bwrite.WriteNBits(7, 1); bwrite.Dispose(); }
private void GenerateByteStream(string filename, int min, int max, bool random) { var fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write); var bitWriter = new BitWriter(fileStream); var rand = new Random(); for (var @byte = min; @byte <= max; @byte++) { if (random) { for (var i = 0; i < rand.Next(5000); i++) { for (var _numBitsRemaining = 7; _numBitsRemaining >= 0; _numBitsRemaining--) { bitWriter.Write((@byte >> _numBitsRemaining) & 1); } } } else { for (var _numBitsRemaining = 7; _numBitsRemaining >= 0; _numBitsRemaining--) { bitWriter.Write((@byte >> _numBitsRemaining) & 1); } } } bitWriter.Dispose(); fileStream.Dispose(); }
private void SaveByArithmeticMethod() { const uint numSymbols = ((maxValuePixel * 2) + 1), eof = numSymbols - 1; ArithmeticCodingLogic arithmCoder = new ArithmeticCodingLogic(bitWriterContext: _bitwriter, total_symbols: numSymbols, eof: eof); for (int i = 0; i < _imgLength; i++) { for (int j = 0; j < _imgLength; j++) { uint symbol = (uint)(_imgPred._errorPredQ[i, j] + (maxValuePixel)); arithmCoder.EncodeImageErrorValue(symbol); } } arithmCoder.SendLastDetailsOfImageError(); _bitwriter.Dispose(); }
private void DecodeExecute_Click(object sender, EventArgs e) { bRead = new BitReader(FilePath.FullName); uint offsetDecoded = bRead.ReadNBits(4); uint lengthDecoded = bRead.ReadNBits(3); var tokens = readTokens(offsetDecoded, lengthDecoded); bRead.Dispose(); lz77Obj.SetOffsetSize((int)offsetDecoded); lz77Obj.SetLengthSize((int)lengthDecoded); var decodedCodes = lz77Obj.Decode(tokens); if (decodeCheckBox.Checked) { foreach (var token in tokens) { log.AppendText("[Decoded]Token: (" + token.offset + ", " + token.length + ", " + token.symbol + ")\n"); } log.AppendText("\n[Decoded]LZ77 codes: \n"); foreach (var code in decodedCodes) { log.AppendText("[Decoded]Code: " + code + "\n"); } log.AppendText("\n"); } string extension = ""; bool foundDot = false; foreach (char c in FilePath.Name) { if (c == '.' && !foundDot) { foundDot = true; } else if (c == '.' && foundDot) { break; } if (foundDot) { extension += c; } } string fileName = FilePath.Name + "." + offsetDecoded.ToString() + "_" + lengthDecoded.ToString() + ".LZW" + extension; bWrite = new BitWriter(Path.Combine(FilePath.DirectoryName, fileName)); foreach (var code in decodedCodes) { bWrite.WriteNBits(8, code); } bWrite.Dispose(); }
public void BitWriterTest2() { var stream = new FileStream("BitWriterTest2.out", FileMode.Create, FileAccess.Write); var bitWriter = new BitWriter(stream); bitWriter.Write(0); bitWriter.Write(1); bitWriter.Dispose(); stream.Dispose(); }
private void EncodeBttn_Click(object sender, EventArgs e) { bitReader = new BitReader(FilePath.FullName); var readBytes = readBytesFromFilePath(8, true); bitReader.Dispose(); lzw.SetDictionarySize(Convert.ToInt32(indexMenu.Text)); if (methodType.SelectedIndex == 0) { lzw.SetDictionaryMethod(Lzw.FREEZE); } else { lzw.SetDictionaryMethod(Lzw.EMPTY); } byte[] byteArray = readBytes.Select(x => (byte)x).ToArray(); var encodedList = lzw.Encode(byteArray); if (codeCheckBox.Checked) { // foreach (var item in readBytes) // log.AppendText(item + ","); log.AppendText("[Encoded]LZW codes: \n"); foreach (var code in encodedList) { log.AppendText("[Encoded]Code: " + code + "\n"); } log.AppendText("\n"); } string fileName = FilePath.Name + "." + methodType.Text + "_" + indexMenu.Text + ".LZW"; bitWriter = new BitWriter(Path.Combine(FilePath.DirectoryName, fileName)); bitWriter.WriteNBits(4, Convert.ToUInt32(indexMenu.Text)); if (methodType.SelectedIndex == 0) { bitWriter.WriteNBits(1, 1); // freeze } else { bitWriter.WriteNBits(1, 0); // empty } foreach (var bytes in encodedList) { bitWriter.WriteNBits(Convert.ToInt32(indexMenu.Text), (uint)bytes); } bitWriter.WriteNBits(7, 1); bitWriter.Dispose(); }
private void EncTxtBoxBttn_Click(object sender, EventArgs e) { byte[] textBoxContentBytes = Encoding.ASCII.GetBytes(encTxtBox.Text); List <byte> huffCodes = huff.Encode(textBoxContentBytes); if (logCheckBox.Checked) { var modelCharacters = huff.GetModel(); foreach (var item in new SortedDictionary <byte, List <byte> >(modelCharacters)) { log.AppendText(item.Key + ": "); foreach (var code in item.Value) { log.AppendText(code.ToString()); } log.AppendText("\n"); } } string fileName = "InputArea.hs"; bWrite = new BitWriter(fileName); var statistic = huff.GetStatistic(); var sortedStatistic = new SortedDictionary <byte, int>(statistic); for (int i = 0; i <= 255; i++) { if (sortedStatistic.ContainsKey((byte)i)) { bWrite.WriteNBits(1, 1); } else { bWrite.WriteNBits(1, 0); } } foreach (var item in sortedStatistic) { bWrite.WriteNBits(8, (uint)item.Value); } foreach (var code in huffCodes) { bWrite.WriteNBits(8, code); } bWrite.WriteNBits(7, 1); bWrite.Dispose(); }
public void Should_throw_an_exception_on_attempt_of_using_a_disposed_object() { var stream = new MemoryStream(); var bitWriter = new BitWriter(stream); bitWriter.Dispose(); Action action = () => bitWriter.WriteBit(true); action .Should() .Throw <ObjectDisposedException>() .WithMessage($"Cannot access a disposed object.\nObject name: '{nameof(BitWriter)}'."); }
/// <summary> /// Flush any unwritten bits and dispose. /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(Boolean disposing) { if (IsDisposed) { return; } IsDisposed = true; if (disposing) { // Dispose managed state (managed objects) Output?.Dispose(); } }
/// <summary> /// Writes the given packet struct to a memory stream. /// </summary> /// <param name="packet">Packet struct to write.</param> /// <typeparam name="T">Type of the packet.</typeparam> /// <returns>Memory stream of the packet to write to the network.</returns> public static MemoryStream WritePacket <T>(T packet) where T : struct { // Create the bit writer. var stream = new MemoryStream(); var bitWriter = new BitWriter(stream, leaveOpen: true); // Write the properties. var writtenProperties = new Dictionary <string, object>(); GetPacketProperties(typeof(T)).Write(packet, bitWriter, writtenProperties); // Return the stream. bitWriter.Dispose(); return(stream); }
private void GenerateByteStream() { var fileStream = new FileStream("FrequencyTableTest.in", FileMode.Create, FileAccess.Write); var bitWriter = new BitWriter(fileStream); for (var @byte = 0; @byte <= byte.MaxValue; @byte++) { for (var _numBitsRemaining = 7; _numBitsRemaining >= 0; _numBitsRemaining--) { bitWriter.Write((@byte >> _numBitsRemaining) & 1); } } bitWriter.Dispose(); fileStream.Dispose(); }
private void EncodeExecute_Click(object sender, EventArgs e) { bRead = new BitReader(FilePath.FullName); var readBytes = readBytesFromFilePath(8); bRead.Dispose(); lz77Obj.SetOffsetSize(Convert.ToInt32(offsetSizeBox.Text)); lz77Obj.SetLengthSize(Convert.ToInt32(lengthSizeBox.Text)); byte[] byteArray = readBytes.Select(x => (byte)x).ToArray(); var encodedList = lz77Obj.Encode(byteArray); if (encodeCheckBox.Checked) { foreach (var bytes in readBytes) { log.AppendText("[Encode]Read byte: " + bytes + "\n"); } log.AppendText("\n[Encoded]LZ77 tokens: \n"); foreach (var token in encodedList) { log.AppendText("[Encoded]Token: (" + token.offset + ", " + token.length + ", " + token.symbol + ")\n"); } log.AppendText("\n"); } string fileName = FilePath.Name + "." + offsetSizeBox.Text + "_" + lengthSizeBox.Text + ".LZ77"; bWrite = new BitWriter(Path.Combine(FilePath.DirectoryName, fileName)); bWrite.WriteNBits(4, Convert.ToUInt32(offsetSizeBox.Text)); bWrite.WriteNBits(3, Convert.ToUInt32(lengthSizeBox.Text)); foreach (var token in encodedList) { bWrite.WriteNBits(Convert.ToInt32(offsetSizeBox.Text), (uint)token.offset); bWrite.WriteNBits(Convert.ToInt32(lengthSizeBox.Text), (uint)token.length); bWrite.WriteNBits(8, (uint)token.symbol); } bWrite.WriteNBits(7, 1); bWrite.Dispose(); }
private void SaveDecBttn_Click(object sender, EventArgs e) { string outputName = filePath.Name + ".decoded"; bwrite = new BitWriter(Path.Combine(filePath.DirectoryName, outputName)); foreach (var item in bmpHeader) { bwrite.WriteNBits(8, item); } for (int i = imgLength - 1; i >= 0; i--) { for (int j = 0; j < imgLength; j++) { bwrite.WriteNBits(8, decodedImg[j, i]); } } //bwrite.WriteNBits(7, 1); bwrite.Dispose(); }
private void EncFileBttn_Click(object sender, EventArgs e) { List <byte> inputList = new List <byte>(); bRead = new BitReader(FilePath.FullName); long fileSize = 8 * new FileInfo(FilePath.FullName).Length; log.AppendText("File Size: " + fileSize + " bits\n"); do { int readBits = 8; if (readBits > fileSize) { readBits = (int)fileSize; } uint value = bRead.ReadNBits(readBits); inputList.Add(Convert.ToByte(value)); fileSize -= readBits; } while (fileSize > 0); bRead.Dispose(); List <byte> huffCodes = huff.Encode(inputList.ToArray()); if (logCheckBox.Checked) { var modelCharacters = huff.GetModel(); foreach (var item in new SortedDictionary <byte, List <byte> >(modelCharacters)) { log.AppendText(item.Key + ": "); foreach (var code in item.Value) { log.AppendText(code.ToString()); } log.AppendText("\n"); } } string fileName = FilePath.Name + ".hs"; bWrite = new BitWriter(Path.Combine(FilePath.DirectoryName, fileName)); var statistic = huff.GetStatistic(); var sortedStatistic = new SortedDictionary <byte, int>(statistic); for (int i = 0; i <= 255; i++) { if (sortedStatistic.ContainsKey((byte)i)) { bWrite.WriteNBits(1, 1); } else { bWrite.WriteNBits(1, 0); } } foreach (var item in sortedStatistic) { bWrite.WriteNBits(8, (uint)item.Value); } foreach (var code in huffCodes) { bWrite.WriteNBits(8, code); } bWrite.WriteNBits(7, 1); bWrite.Dispose(); }
private void DecFileBttn_Click(object sender, EventArgs e) { bRead = new BitReader(FilePath.FullName); List <byte> statsList = new List <byte>(); Dictionary <byte, int> statistic = new Dictionary <byte, int>(); for (int i = 0; i < 256; i++) { uint value = bRead.ReadNBits(1); if (value == 1) { statsList.Add((byte)i); } } int length = statsList.Count; for (int i = 0; i < length; i++) { uint nr = bRead.ReadNBits(8); statistic.Add(statsList[i], (int)nr); } List <byte> codedValues = new List <byte>(); long fileSize = 8 * new FileInfo(FilePath.FullName).Length - (256 + (length * 8)); log.AppendText("File Size: " + fileSize + " bits\n"); do { int readBits = 8; if (readBits > fileSize) { readBits = (int)fileSize; } uint value = bRead.ReadNBits(readBits); codedValues.Add(Convert.ToByte(value)); fileSize -= readBits; } while (fileSize > 0); bRead.Dispose(); var decodedValues = huff.Decode(codedValues, statistic); if (FilePath.Name.Equals("InputArea.hs")) { string fileName = "InputArea.txt"; bWrite = new BitWriter(fileName); } else { string extension = ""; bool foundDot = false; foreach (char c in FilePath.Name) { if (c == '.' && !foundDot) { foundDot = true; } else if (c == '.' && foundDot) { break; } if (foundDot) { extension += c; } } string fileName = FilePath.Name + extension; bWrite = new BitWriter(Path.Combine(FilePath.DirectoryName, fileName)); } foreach (var item in decodedValues) { bWrite.WriteNBits(8, item); } bWrite.Dispose(); }
private void DecodeBttn_Click(object sender, EventArgs e) { bitReader = new BitReader(FilePath.FullName); uint size = bitReader.ReadNBits(4); uint method = bitReader.ReadNBits(1); var readByteArray = readBytesFromFilePath((int)size, false); bitReader.Dispose(); var byteList = readByteArray.Select(x => (int)x).ToList(); lzw.SetDictionarySize((int)size); if (method == 1) { lzw.SetDictionaryMethod(Lzw.FREEZE); } else if (method == 0) { lzw.SetDictionaryMethod(Lzw.EMPTY); } else { log.AppendText("ERROR at Decode!!!"); } var decodedCodes = lzw.Decode(byteList); if (decodeCheckBox.Checked) { log.AppendText("[Decoded]LZW codes: \n"); foreach (var code in decodedCodes) { log.AppendText("[Decoded]Code: " + code + "\n"); } log.AppendText("\n"); } string methodString = ""; if (method == 1) { methodString = "Freeze"; } else { methodString = "Empty"; } string extension = ""; bool foundDot = false; foreach (char c in FilePath.Name) { if (c == '.' && !foundDot) { foundDot = true; } else if (c == '.' && foundDot) { break; } if (foundDot) { extension += c; } } string fileName = FilePath.Name + "." + methodString + "_" + size.ToString() + ".LZW" + extension; bitWriter = new BitWriter(Path.Combine(FilePath.DirectoryName, fileName)); foreach (var code in decodedCodes) { bitWriter.WriteNBits(8, code); } bitWriter.Dispose(); }