public Image <TPixel> ReadPict <TPixel>(Configuration configuration, string filename) where TPixel : unmanaged, SixLabors.ImageSharp.PixelFormats.IPixel <TPixel> { using Magick.MagickImage magickImage = new Magick.MagickImage(filename); var width = magickImage.Width; var height = magickImage.Height; var image = new Image <TPixel>(configuration, width, height); var framePixels = image.GetPixelMemoryGroup(); using Magick.IUnsafePixelCollection <ushort> pixels = magickImage.GetPixelsUnsafe(); if (magickImage.Depth == 8 || magickImage.Depth == 6 || magickImage.Depth == 4 || magickImage.Depth == 2 || magickImage.Depth == 1 || magickImage.Depth == 10 || magickImage.Depth == 12) { byte[]? data = pixels.ToByteArray(Magick.PixelMapping.RGBA); FromRgba32Bytes(configuration, data, framePixels); } else if (magickImage.Depth == 16 || magickImage.Depth == 14) { ushort[]? data = pixels.ToShortArray(Magick.PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); FromRgba64Bytes(configuration, bytes, framePixels); } else { throw new InvalidOperationException(); } return(image); }
/// <summary> /// Returns a decoded image from ImageMagick - assumes only a singal frame, /// as many methods I need are internal to ImageSharp /// </summary> /// <typeparam name="TPixel"></typeparam> /// <param name="configuration"></param> /// <param name="stream"></param> /// <returns></returns> public Image <TPixel> Decode <TPixel>(Configuration configuration, Stream stream) where TPixel : unmanaged, SixLabors.ImageSharp.PixelFormats.IPixel <TPixel> { var settings = new Magick.MagickReadSettings(); using var magickImageCollection = new Magick.MagickImageCollection(stream, settings); if (magickImageCollection.Count == 0) { throw new Exception("No images found"); } Magick.IMagickImage <ushort> magickImage = magickImageCollection[0]; var width = magickImage.Width; var height = magickImage.Height; var image = new Image <TPixel>(configuration, width, height); var framePixels = image.GetPixelMemoryGroup(); using Magick.IUnsafePixelCollection <ushort> pixels = magickImage.GetPixelsUnsafe(); if (magickImage.Depth == 8 || magickImage.Depth == 6 || magickImage.Depth == 4 || magickImage.Depth == 2 || magickImage.Depth == 1 || magickImage.Depth == 10 || magickImage.Depth == 12) { byte[]? data = pixels.ToByteArray(Magick.PixelMapping.RGBA); FromRgba32Bytes(configuration, data, framePixels); } else if (magickImage.Depth == 16 || magickImage.Depth == 14) { ushort[]? data = pixels.ToShortArray(Magick.PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); FromRgba64Bytes(configuration, bytes, framePixels); } else { throw new InvalidOperationException(); } return(image); }