public virtual void Storage_InputStreamStoringInMostSignifcantBit() { byte[] embeddedData = { 100, 200, 250, 0, 5, 6, 10, 99 }; PixelStorageOptions storageOptions = new PixelStorageOptions(new[] { new PixelStorageOptions.ChannelStorageOptions(1, PixelStorageOptions.BitStorageMode.MostSignificantBits), }); var outputStream = new MemoryStream(); using (Stream pixelWriterStream = new PixelStorageWriterStream(outputStream, new MemoryStream(embeddedData), storageOptions, false)) { pixelWriterStream.WriteByte(255); byte[] output = outputStream.ToArray(); CollectionAssert.AreEqual( new byte[] { 228, 200, 250, 128, 133, 134, 138, 227 }, output); using (Stream imageReaderStream = new PixelStorageReaderStream(new MemoryStream(output), storageOptions, false)) { var decodedData = new MemoryStream(); imageReaderStream.CopyTo(decodedData); CollectionAssert.AreEqual(new byte[] { 255 }, decodedData.ToArray()); } } }
public virtual async Task Storage_InputStreamThenOuputStreamProducesEquivalentData(Stream testData, PixelStorageOptions pixelStorageOptions, Stream image) { testData.Position = 0; var originalDataStream = new MemoryStream(); await testData.CopyToAsync(originalDataStream); originalDataStream.Position = 0; var pixelDataStream = new MemoryStream(); byte[] paddingBytes; using (var pixelWriterStream = new PixelStorageWriterStream(pixelDataStream, image, pixelStorageOptions, true)) { paddingBytes = new byte[pixelWriterStream.BytesPerUnit - originalDataStream.Length % pixelWriterStream.BytesPerUnit]; // Use an uneven write size to ensure remainder bytes are handled properly while (originalDataStream.Length != originalDataStream.Position) { foreach (int writeSize in writeSizes) { byte[] buffer = new byte[writeSize]; int totalBytesRead = 0; while (totalBytesRead < writeSize) { int bytesRead = await originalDataStream.ReadAsync(buffer, totalBytesRead, buffer.Length - totalBytesRead); if (bytesRead == 0) { break; } totalBytesRead += bytesRead; } await pixelWriterStream.WriteAsync(buffer, 0, totalBytesRead); } } await pixelWriterStream.WriteAsync(paddingBytes, 0, paddingBytes.Length); } var decodedData = new MemoryStream(); pixelDataStream.Position = 0; using (var imageReaderStream = new PixelStorageReaderStream(pixelDataStream, pixelStorageOptions, true)) { await imageReaderStream.CopyToAsync(decodedData, 4096); } byte[] expectedData = originalDataStream.ToArray().Concat(paddingBytes).ToArray(); var actualData = decodedData.ToArray(); if (!expectedData.SequenceEqual(actualData)) { CollectionAssert.AreEqual(expectedData, actualData); Assert.Fail(); } }
private async Task WriteEmbeddedImagePaddingAsync(PixelStorageWriterStream pixelStorageStream) { var embeddedImageDataStream = pixelStorageStream.EmbeddedImageDataStream; if (pixelStorageStream.BytesLeftInUnit != 0) { // Pad the final unit of the pixel storage stream // Cannot pad with embedded image data stream as the // stream will also be read from within the storage stream // advancing the image stream too far. await pixelStorageStream.WriteAsync(new byte[pixelStorageStream.BytesLeftInUnit], 0, pixelStorageStream.BytesLeftInUnit); } // Directly copy remaining image data to underlying image stream. await new EmbeddedImagePadding(embeddedImageDataStream, EncodingConfiguration.BufferSize) .PadDataAsync(pixelStorageStream.ImageFormatterStream); }