< Summary

Class:Imagini.ImageSharp.SurfaceExtensions
Assembly:Imagini.ImageSharp
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.ImageSharp/SurfaceExtensions.cs
Covered lines:18
Uncovered lines:4
Coverable lines:22
Total lines:66
Line coverage:81.8% (18 of 22)
Covered branches:4
Total branches:4
Branch coverage:100% (4 of 4)

Metrics

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

File(s)

/home/razer/vscode-projects/project-grove/imagini/Imagini.ImageSharp/SurfaceExtensions.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 Surface-related extensions.
 13    /// </summary>
 14    public static class SurfaceExtensions
 15    {
 16        /// <summary>
 17        /// Saves the surface to the specified stream.
 18        /// </summary>
 19        public static void SaveAsPng(this Surface surface, Stream stream) =>
 420            surface.Save(image => image.SaveAsPng(stream));
 21
 22        /// <summary>
 23        /// Saves the surface to the specified file, overwriting it if it exists.
 24        /// </summary>
 25        public static void SaveAsPng(this Surface surface, string path)
 26        {
 127            using(var stream = new FileStream(path, FileMode.Create))
 128                surface.SaveAsPng(stream);
 129        }
 30
 31        /// <summary>
 32        /// Saves the surface using the specified save action.
 33        /// </summary>
 34        /// <example>
 35        /// surface.Save(image => image.SaveAsJpeg("file.jpg"))
 36        /// </example>
 37        public static void Save(this Surface surface,
 38            Action<Image<Rgba32>> onSave)
 39        {
 240            var mustDispose = false;
 241            var source = surface;
 242            var targetFormat = PixelFormat.Format_ABGR8888;
 243            if (surface.PixelInfo.Format != targetFormat)
 44            {
 245                source = surface.ConvertTo(targetFormat);
 246                mustDispose = true;
 47            }
 48
 249            var image = new Image<Rgba32>(surface.Width, surface.Height);
 250            unsafe {
 251                fixed(void* target = &MemoryMarshal.GetReference(image.GetPixelSpan()))
 52                {
 053                    Buffer.MemoryCopy(
 054                        (void*)source.PixelsHandle,
 055                        target,
 056                        surface.SizeInBytes, surface.SizeInBytes);
 57                }
 58            }
 259            onSave(image);
 60
 261            image.Dispose();
 262            if (mustDispose)
 263                source.Dispose();
 264        }
 65    }
 66}