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(); }
public BmpWvlFileObject(string filePath, bool isWaveletFile) { if (isWaveletFile == false) { _image = new Bitmap(512, 512); _dataContainer = new byte[512, 512]; BitReader reader = new BitReader(filePath); for (int i = 0; i < _headerContainer.Length; i++) { _headerContainer[i] = (byte)reader.ReadNBits(8); } for (int i = 511; i >= 0; i--) { for (int j = 0; j < 512; j++) { _dataContainer[j, i] = (byte)reader.ReadNBits(8); _image.SetPixel(j, i, Color.FromArgb(_dataContainer[j, i], _dataContainer[j, i], _dataContainer[j, i])); } } reader.Dispose(); } else { _waveletContainer = LoadWaveletFromFile(filePath); } }
public void RandomBitReaderTest() { Random rand = new Random(); byte[] buffer = new byte[50]; rand.NextBytes(buffer); unsafe { fixed(byte *ptr = buffer) { var reader = new BitReader(ptr, 0); reader.ReadBit(); reader.ReadBits(10); reader.ReadBits(15); reader.SeekBits(11); reader.ReadInt64(); reader.ReadUInt32(); reader.ReadUInt16(); reader.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(); }
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(); }
public unsafe void RandomBitReaderTest0() { Random rand = new Random(); byte[] buffer = new byte[rand.Next(50, 100)]; rand.NextBytes(buffer); fixed(byte *ptr = buffer) { BitReader reader = new BitReader(ptr, 0); reader.Dispose(); } }
public void Should_throw_an_exception_on_attempt_of_using_a_disposed_object() { var stream = new MemoryStream(); var bitReader = new BitReader(stream); bitReader.Dispose(); Action action = () => bitReader.ReadBit(); action .Should() .Throw <ObjectDisposedException>() .WithMessage($"Cannot access a disposed object.\nObject name: '{nameof(BitReader)}'."); }
/// <summary> /// Dispose. /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(Boolean disposing) { if (IsDisposed) { return; } IsDisposed = true; if (disposing) { // Dispose managed state (managed objects) Input?.Dispose(); } }
public void BitReaderTest1() { var stream = new FileStream("BitWriterTest1.out", FileMode.Open, FileAccess.Read); var bitReader = new BitReader(stream); var bit = bitReader.Read(); while (bit != -1) { Console.Write(bit); bit = bitReader.Read(); } bitReader.Dispose(); stream.Dispose(); }
/// <summary> /// Creates the given packet struct from the given memory stream. /// </summary> /// <param name="packetType">Type of the packet to read.</param> /// <param name="stream">Stream to read from.</param> /// <param name="context">Properties that provide context for reading, such as world zone ids.</param> /// <returns>Packet object that was read.</returns> public static object ReadPacket(Type packetType, Stream stream, Dictionary <string, object> context = null) { // Create the bit reader. var bitReader = new BitReader(stream); // Read the properties. var packet = Activator.CreateInstance(packetType); var writtenProperties = new Dictionary <string, object>(); GetPacketProperties(packetType).Read(packet, bitReader, writtenProperties, context); // Return the packet. bitReader.Dispose(); return(packet); }
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 StoreBttn_Click(object sender, EventArgs e) { _bitReader = new BitReader(_filePath.FullName); _bmpHeader = new List <uint>(readBMPHeader()); _bitReader.Dispose(); string outputName = _filePath.Name + ".k" + textBoxKVal.Text + "p" + _imgPred._predictionNumber + comboBoxSaveMode.Text.Substring(0, 1) + ".nlp"; _bitwriter = new BitWriter(Path.Combine(_filePath.DirectoryName, outputName)); // write header foreach (var item in _bmpHeader) { _bitwriter.WriteNBits(8, item); } // predictor used _bitwriter.WriteNBits(4, (uint)_imgPred._predictionNumber); // k number _bitwriter.WriteNBits(4, (uint)_imgPred._k); // F/T/A mode used _bitwriter.WriteNBits(2, (uint)comboBoxSaveMode.SelectedIndex); switch (comboBoxSaveMode.SelectedIndex) { case 0: SaveByFixedMethod(); // 9 bits break; case 1: SaveByJpegTable(); break; case 2: SaveByArithmeticMethod(); break; } }
private void DecodeBttn_Click(object sender, EventArgs e) { bread = new BitReader(filePath.FullName); bmpHeader = readBMPHeader(); int fileMethod = (int)bread.ReadNBits(4); var errorList = readErrorMatrix(); bread.Dispose(); int[,] errorMatrix = new int[imgLength, imgLength]; int k = 0; for (int i = 0; i < imgLength; i++) { for (int j = 0; j < imgLength; j++) { errorMatrix[i, j] = (int)errorList[k]; k++; } } imgPred = new ImagePredictor(); imgPred.Decode(errorMatrix, methodName: fileMethod); decodedImg = imgPred.origDecodedMatrix; DecodedPBox.Image = new Bitmap(imgLength, imgLength); for (int i = 0; i < imgLength; i++) { for (int j = 0; j < imgLength; j++) { int color = decodedImg[i, j]; ((Bitmap)DecodedPBox.Image).SetPixel(i, j, Color.FromArgb(color, color, color)); } } //DecodedPBox.Image.Save(filePath.DirectoryName + "\\decodedImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp); }
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(); }
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 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 DecodeBttn_Click(object sender, EventArgs e) { _bitReader = new BitReader(_filePath.FullName); _bmpHeader = readBMPHeader(); int fileMethod = (int)_bitReader.ReadNBits(4); int k = (int)_bitReader.ReadNBits(4); int saveMode = (int)_bitReader.ReadNBits(2); List <int> errorList = new List <int>(); switch (saveMode) { case 0: errorList = new List <int>(readFixedCompressedFile()); break; case 1: errorList = new List <int>(readJpegTableCompressedFile()); break; case 2: errorList = new List <int>(readArithmeticCompressedFile()); break; } _bitReader.Dispose(); if (!errorList.Any()) { throw new Exception("Something didn't work correctly at decoding!"); } int[,] errorPredQ = new int[_imgLength, _imgLength]; // from List to matrix conversion int index = 0; for (int i = 0; i < _imgLength; i++) { for (int j = 0; j < _imgLength; j++) { errorPredQ[i, j] = errorList[index]; index++; } } _imgPred = new ImagePredictor(k: k); _imgPred.Decode(errorPredQ, methodNumber: fileMethod); _decodedImg = _imgPred._decod; DecodedPBox.Image = new Bitmap(_imgLength, _imgLength); for (int i = 0; i < _imgLength; i++) { for (int j = 0; j < _imgLength; j++) { int color = _decodedImg[i, j]; ((Bitmap)DecodedPBox.Image).SetPixel(i, j, Color.FromArgb(color, color, color)); } } //DecodedPBox.Image.Save(filePath.DirectoryName + "\\decodedImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp); }