< Summary

Class:Imagini.Drawing.PixelFormatInfo
Assembly:Imagini.2D
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.2D/Drawing/PixelFormatInfo.cs
Covered lines:39
Uncovered lines:5
Coverable lines:44
Total lines:119
Line coverage:88.6% (39 of 44)
Covered branches:6
Total branches:10
Branch coverage:60% (6 of 10)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)2083.33%50%
.ctor(...)10100%100%
FromSDL(...)20100%100%
Destroy()20100%50%
Dispose()10100%100%
.cctor()10100%100%

File(s)

/home/razer/vscode-projects/project-grove/imagini/Imagini.2D/Drawing/PixelFormatInfo.cs

#LineLine coverage
 1using System;
 2using static SDL2.SDL_error;
 3using static SDL2.SDL_pixels;
 4using static Imagini.ErrorHandler;
 5using System.Runtime.InteropServices;
 6
 7namespace Imagini.Drawing
 8{
 9    /// <summary>
 10    /// Contains pixel format information.
 11    /// </summary>
 12    public sealed class PixelFormatInfo : Resource, IDisposable
 13    {
 14        internal IntPtr Handle;
 15
 16        /// <summary>
 17        /// Gets the pixel format.
 18        /// </summary>
 28619        public PixelFormat Format { get; private set; }
 20        /// <summary>
 21        /// Returns bits per pixel.
 22        /// </summary>
 11823        public int BitsPerPixel { get; private set; }
 24        /// <summary>
 25        /// Returns bytes per pixel.
 26        /// </summary>
 9427        public int BytesPerPixel { get; private set; }
 28
 29        /// <summary>
 30        /// A bit mask representing the location of the red component of the pixel.
 31        /// </summary>
 11632        public int MaskR { get; private set; }
 33        /// <summary>
 34        /// A bit mask representing the location of the green component of the pixel.
 35        /// </summary>
 11636        public int MaskG { get; private set; }
 37        /// <summary>
 38        /// A bit mask representing the location of the blue component of the pixel.
 39        /// </summary>
 11640        public int MaskB { get; private set; }
 41        /// <summary>
 42        /// A bit mask representing the location of the alpha component of the
 43        /// pixel or 0 if the pixel format doesn't have any alpha information.
 44        /// </summary>
 11645        public int MaskA { get; private set; }
 46
 47        private Palette _palette;
 48        /// <summary>
 49        /// Gets or sets the palette used by this pixel format. Returns null
 50        /// if the palette is not present.
 51        /// </summary>
 52        public Palette Palette
 53        {
 54            get
 55            {
 656                return _palette;
 57            }
 58            set
 59            {
 160                if (!Format.IsIndexed())
 061                    throw new ImaginiException("Only indexed formats can have palettes");
 062                Try(() =>
 163                    SDL_SetPixelFormatPalette(Handle, value.Handle),
 064                    "SDL_SetPixelFormatPalette");
 165                if (_palette != null)
 066                    Unregister(_palette);
 167                _palette = value;
 168                Register(_palette);
 169            }
 70        }
 71
 72        /// <summary>
 73        /// Creates a new PixelFormatInfo with the specified pixel format.
 74        /// </summary>
 3675        public PixelFormatInfo(PixelFormat format)
 76        {
 3677            Handle = SDL_AllocFormat((uint)format);
 3678            if (Handle == IntPtr.Zero)
 079                throw new ImaginiException($"Could not create a pixel format: {SDL_GetError()}");
 3680            FromSDL(Marshal.PtrToStructure<SDL_PixelFormat>(Handle));
 3681        }
 82
 4283        internal PixelFormatInfo(IntPtr handle)
 84        {
 4285            Handle = handle;
 4286            FromSDL(Marshal.PtrToStructure<SDL_PixelFormat>(Handle));
 4287        }
 88
 89        private void FromSDL(SDL_PixelFormat fmt)
 90        {
 7891            if (fmt.palette != IntPtr.Zero)
 192                _palette = new Palette(fmt.palette);
 7893            Format = (PixelFormat)fmt.format;
 7894            BitsPerPixel = Format.GetBitsPerPixel();
 7895            BytesPerPixel = Format.GetBytesPerPixel();
 96            unchecked
 97            {
 7898                MaskR = (int)fmt.Rmask;
 7899                MaskG = (int)fmt.Gmask;
 78100                MaskB = (int)fmt.Bmask;
 78101                MaskA = (int)fmt.Amask;
 102            }
 78103        }
 104
 105        internal override void Destroy()
 106        {
 35107            if (IsDisposed) return;
 35108            base.Destroy();
 35109            SDL_FreeFormat(Handle);
 35110        }
 111
 112        /// <summary>
 113        /// Disposes the object.
 114        /// </summary>
 35115        public void Dispose() => Destroy();
 116
 1117        static PixelFormatInfo() => Lifecycle.TryInitialize();
 118    }
 119}