< Summary

Class:Imagini.ImageSharp.GraphicsExtensions
Assembly:Imagini.ImageSharp
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.ImageSharp/GraphicsExtensions.cs
Covered lines:15
Uncovered lines:9
Coverable lines:24
Total lines:69
Line coverage:62.5% (15 of 24)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
SaveAsPng(...)10100%100%
SaveAsPng(...)100%100%
Save(...)1070%100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Runtime.InteropServices;
 4using Imagini.Drawing;
 5using SixLabors.ImageSharp;
 6using SixLabors.ImageSharp.Advanced;
 7using SixLabors.ImageSharp.PixelFormats;
 8
 9namespace Imagini.ImageSharp
 10{
 11    /// <summary>
 12    /// Contains various Graphics-related extensions.
 13    /// </summary>
 14    public static class GraphicsExtensions
 15    {
 16        /// <summary>
 17        /// Saves the graphics to the specified stream.
 18        /// </summary>
 19        public static void SaveAsPng(this Graphics graphics, Stream stream) =>
 1220            graphics.Save(image => image.SaveAsPng(stream));
 21
 22        /// <summary>
 23        /// Saves the graphics to the specified file, overwriting it if it exists.
 24        /// </summary>
 25        public static void SaveAsPng(this Graphics graphics, string path)
 26        {
 027            using (var stream = new FileStream(path, FileMode.Create))
 028                graphics.SaveAsPng(stream);
 029        }
 30
 31        /// <summary>
 32        /// Saves the graphics using the specified save action.
 33        /// </summary>
 34        /// <example>
 35        /// graphics.Save(image => image.SaveAsJpeg("file.jpg"))
 36        /// </example>
 37        public static void Save(this Graphics graphics,
 38            Action<Image<Rgba32>> onSave)
 39        {
 640            var targetFormat = PixelFormat.Format_ABGR8888;
 641            var size = graphics.OutputSize;
 642            var pixelData = new ColorRGB888[graphics.PixelCount];
 643            graphics.ReadPixels(ref pixelData);
 644            var pixelHandle = GCHandle.Alloc(pixelData, GCHandleType.Pinned);
 45            try
 46            {
 647                var image = new Image<Rgba32>(size.Width, size.Height);
 48                unsafe
 649                {
 650                    fixed (void* target = &MemoryMarshal.GetReference(image.GetPixelSpan()))
 51                    {
 052                        Pixels.Convert(size.Width, size.Height,
 053                            4 * size.Width, 4 * size.Width,
 054                            PixelFormat.Format_RGB888,
 055                            targetFormat,
 056                            pixelHandle.AddrOfPinnedObject(),
 057                            (IntPtr)target);
 58                    }
 59                }
 660                onSave(image);
 661                image.Dispose();
 662            }
 63            finally
 64            {
 665                pixelHandle.Free();
 666            }
 667        }
 68    }
 69}