コード例 #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);
        }
コード例 #2
0
        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);
         }
     }
 }
コード例 #6
0
            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);
                    }
                }
            }
コード例 #7
0
 public void ShouldThrowExceptionWhenMappingIsInvalid()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.Throws <MagickOptionErrorException>(() =>
             {
                 pixels.ToShortArray("FOO");
             });
         }
     }
 }
コード例 #8
0
 public void ShouldThrowExceptionWhenMappingIsEmpty()
 {
     using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             ExceptionAssert.ThrowsArgumentException("mapping", () =>
             {
                 pixels.ToShortArray(string.Empty);
             });
         }
     }
 }
コード例 #9
0
            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);
                }
            }
        }