< Summary

Class:Imagini.ImageSharp.SurfaceFactory
Assembly:Imagini.ImageSharp
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.ImageSharp/SurfaceFactory.cs
Covered lines:14
Uncovered lines:4
Coverable lines:18
Total lines:61
Line coverage:77.7% (14 of 18)
Covered branches:2
Total branches:2
Branch coverage:100% (2 of 2)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
FromFile(...)10100%100%
FromStream(...)10100%100%
FromImage(...)2073.33%100%

File(s)

/home/razer/vscode-projects/project-grove/imagini/Imagini.ImageSharp/SurfaceFactory.cs

#LineLine coverage
 1using System.Buffers;
 2using System.IO;
 3using System.Linq;
 4using Imagini.Drawing;
 5using SixLabors.ImageSharp;
 6using SixLabors.ImageSharp.Advanced;
 7using SixLabors.ImageSharp.Formats;
 8using SixLabors.ImageSharp.PixelFormats;
 9
 10/// <summary>
 11/// ImageSharp integration module.
 12/// </summary>
 13namespace Imagini.ImageSharp
 14{
 15    /// <summary>
 16    /// Contains various functions to aid in surface creation.
 17    /// </summary>
 18    public static class SurfaceFactory
 19    {
 20        /// <summary>
 21        /// Gets or sets the ArrayPool used by image loader.
 22        /// </summary>
 2123        public static ArrayPool<byte> ArrayPool { get; set; } = ArrayPool<byte>.Shared;
 24
 25        /// <summary>
 26        /// Creates a RGBA8888 surface by loading a file from the specified path.
 27        /// </summary>
 428        public static Surface FromFile(string path) => FromImage(Image.Load(path));
 29
 30        /// <summary>
 31        /// Creates a RGBA8888 surface by loading a file from the specified stream.
 32        /// </summary>
 33        public static Surface FromStream(Stream stream, IImageDecoder decoder) =>
 634            FromImage(Image.Load(stream, decoder));
 35
 36        /// <summary>
 37        /// Creates a RGBA8888 surface from the specified Image object.
 38        /// </summary>
 39        public static Surface FromImage<TPixel>(Image<TPixel> image)
 40            where TPixel : struct, IPixel<TPixel>
 41        {
 1042            var cloned = image.CloneAs<Rgba32>();
 1043            var pixels = cloned.GetPixelSpan();
 1044            var bytes = ArrayPool.Rent(pixels.Length * 4);
 9022045            for (int i = 0; i < pixels.Length; i++)
 46            {
 4510047                var pixel = pixels[i];
 4510048                bytes[i * 4] = pixel.A;
 4510049                bytes[i * 4 + 1] = pixel.B;
 4510050                bytes[i * 4 + 2] = pixel.G;
 4510051                bytes[i * 4 + 3] = pixel.R;
 52            }
 053            var surface = Surface.CreateFrom(bytes,
 054                cloned.Width,
 055                cloned.Height,
 056                PixelFormat.Format_RGBA8888);
 1057            ArrayPool.Return(bytes);
 1058            return surface;
 59        }
 60    }
 61}