Пример #1
0
        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);
            }
        }
Пример #3
0
 public void ShouldReturnNullWhenGeometryIsNull()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             var values = pixels.ToShortArray(null, "RGB");
             Assert.IsNull(values);
         }
     }
 }
Пример #4
0
 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");
                 });
             }
         }
     }
 }
Пример #5
0
 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);
                    }
                }
            }
Пример #10
0
            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);
                    }
                }
            }
Пример #11
0
 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);
             });
         }
     }
 }
Пример #12
0
 public void ShouldThrowExceptionWhenGeometryIsNullAndMappingIsEnum()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.ThrowsArgumentNullException("geometry", () =>
             {
                 pixels.ToShortArray(null, PixelMapping.RGB);
             });
         }
     }
 }
Пример #13
0
 public void ToShortArray_GeometryIsNull_ThrowsException()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             ExceptionAssert.ThrowsArgumentNullException("geometry", () =>
             {
                 pixels.ToShortArray(null, "RGB");
             });
         }
     }
 }
Пример #14
0
            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);
                    }
                }
            }
Пример #15
0
 public void ShouldThrowExceptionWhenMappingIsEmpty()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             ExceptionAssert.Throws <MagickResourceLimitErrorException>(() =>
             {
                 pixels.ToShortArray(string.Empty);
             });
         }
     }
 }
Пример #16
0
 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);
             });
         }
     }
 }
Пример #17
0
        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);
                }
            }
        }
Пример #18
0
 public void ToShortArray_MappingIsNull_ThrowsException()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.ThrowsArgumentNullException("mapping", () =>
             {
                 pixels.ToShortArray(null);
             });
         }
     }
 }
Пример #19
0
 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");
             });
         }
     }
 }
Пример #20
0
            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);
                    }
                }
            }
Пример #21
0
 public void ToShortArray_InvalidMapping_ThrowsException()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixelsUnsafe())
         {
             ExceptionAssert.Throws<MagickOptionErrorException>(() =>
             {
                 pixels.ToShortArray("FOO");
             });
         }
     }
 }
Пример #22
0
 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);
             });
         }
     }
 }
Пример #23
0
        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);
                }
            }
        }