public Image <TPixel> Decode <TPixel>(Configuration configuration, Stream stream) where TPixel : unmanaged, IPixel <TPixel> { using var magickImage = new MagickImage(stream); var result = new Image <TPixel>(configuration, magickImage.Width, magickImage.Height); IMemoryGroup <TPixel> resultPixels = result.GetRootFramePixelBuffer().FastMemoryGroup; using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) { if (magickImage.Depth == 8) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); FromRgba32Bytes(configuration, data, resultPixels); } else if (magickImage.Depth == 16) { ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); FromRgba64Bytes(configuration, bytes, resultPixels); } else { throw new InvalidOperationException(); } } return(result); }
public Image <TPixel> Decode <TPixel>(Configuration configuration, Stream stream) where TPixel : struct, IPixel <TPixel> { using (var magickImage = new MagickImage(stream)) { var result = new Image <TPixel>(configuration, magickImage.Width, magickImage.Height); Span <TPixel> resultPixels = result.GetPixelSpan(); using (IPixelCollection pixels = magickImage.GetPixelsUnsafe()) { if (magickImage.Depth == 8) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); PixelOperations <TPixel> .Instance.PackFromRgba32Bytes(data, resultPixels, resultPixels.Length); } else if (magickImage.Depth == 16) { ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); PixelOperations <TPixel> .Instance.PackFromRgba64Bytes(bytes, resultPixels, resultPixels.Length); } else { throw new InvalidOperationException(); } } return(result); } }
public void ShouldReturnNullWhenGeometryIsNull() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var values = pixels.ToShortArray(null, "RGB"); Assert.IsNull(values); } } }
public void ShouldThrowExceptionWhenXTooLow() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { if (Is64Bit) { pixels.ToShortArray(-1, 0, 1, 1, "RGB"); } else { ExceptionAssert.Throws <OverflowException>(() => { pixels.ToShortArray(-1, 0, 1, 1, "RGB"); }); } } } }
public void ShouldReturnNullWhenGeometryIsSpecifiedAndMappingIsNull() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var values = pixels.ToShortArray(new MagickGeometry(1, 2, 3, 4), null); Assert.IsNull(values); } } }
public void ShouldReturnArrayWhenTwoChannelsAreSuppliedAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToShortArray(PixelMapping.RGB); int length = image.Width * image.Height * 3; Assert.AreEqual(length, values.Length); } } }
public void ShouldThrowExceptionWhenMappingIsInvalid() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.Throws <MagickOptionErrorException>(() => { pixels.ToShortArray("FOO"); }); } } }
public void ShouldThrowExceptionWhenMappingIsEmpty() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentException("mapping", () => { pixels.ToShortArray(string.Empty); }); } } }
public void ShouldReturnArrayWhenGeometryIsCorrectAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToShortArray(new MagickGeometry(10, 10, 113, 108), PixelMapping.RGB); var length = 113 * 108 * 3; Assert.AreEqual(length, values.Length); } } }
public void ShouldReturnArrayWhenGeometryIsCorrect() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToShortArray(new MagickGeometry(10, 10, 113, 108), "RG"); int length = 113 * 108 * 2; Assert.AreEqual(length, values.Length); } } }
public void ToShortArray_GeometryIsValidMappingIsEmpty_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentException("mapping", () => { pixels.ToShortArray(new MagickGeometry(1, 2, 3, 4), string.Empty); }); } } }
public void ShouldThrowExceptionWhenGeometryIsNullAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentNullException("geometry", () => { pixels.ToShortArray(null, PixelMapping.RGB); }); } } }
public void ToShortArray_GeometryIsNull_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.ThrowsArgumentNullException("geometry", () => { pixels.ToShortArray(null, "RGB"); }); } } }
public void ShouldReturnArrayWhenTwoChannelsAreSupplied() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var values = pixels.ToShortArray("RG"); int length = image.Width * image.Height * 2; Assert.AreEqual(length, values.Length); } } }
public void ShouldThrowExceptionWhenMappingIsEmpty() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.Throws <MagickResourceLimitErrorException>(() => { pixels.ToShortArray(string.Empty); }); } } }
public void ShouldThrowExceptionWhenGeometryIsSpecifiedAndMappingIsEmpty() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.Throws <MagickResourceLimitErrorException>(() => { var values = pixels.ToShortArray(new MagickGeometry(1, 2, 3, 4), string.Empty); }); } } }
public void ToShortArray_TwoChannels_ReturnsArray() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToShortArray("RG"); int length = image.Width * image.Height * 2; Assert.AreEqual(length, values.Length); } } }
public void ToShortArray_MappingIsNull_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentNullException("mapping", () => { pixels.ToShortArray(null); }); } } }
public void ShouldThrowExceptionWhenXTooLow() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentOutOfRangeException("x", () => { pixels.ToShortArray(-1, 0, 1, 1, "RGB"); }); } } }
public void ShouldReturnPixelsWhenAreaIsCorrectAndMappingIsEnum() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToShortArray(60, 60, 63, 58, PixelMapping.RGBA); int length = 63 * 58 * 4; Assert.AreEqual(length, values.Length); } } }
public void ToShortArray_InvalidMapping_ThrowsException() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { ExceptionAssert.Throws<MagickOptionErrorException>(() => { pixels.ToShortArray("FOO"); }); } } }
public void ShouldThrowExceptionWhenGeometryIsSpecifiedAndMappingIsNull() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { ExceptionAssert.ThrowsArgumentNullException("mapping", () => { pixels.ToShortArray(new MagickGeometry(1, 2, 3, 4), null); }); } } }
public void ToShortArray_AreaIsCorrect_ReturnsArray() { using (IMagickImage image = new MagickImage(Files.ImageMagickJPG)) { using (IPixelCollection pixels = image.GetPixels()) { var values = pixels.ToShortArray(60, 60, 63, 58, "RGBA"); int length = 63 * 58 * 4; Assert.AreEqual(length, values.Length); } } }