public Image <TPixel> Decode <TPixel>(Configuration configuration, Stream stream) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel <TPixel> { var bmpReadDefines = new BmpReadDefines { IgnoreFileSize = !this.validate }; var settings = new MagickReadSettings(); settings.SetDefines(bmpReadDefines); using var magickImageCollection = new MagickImageCollection(stream, settings); var framesList = new List <ImageFrame <TPixel> >(); foreach (IMagickImage <ushort> magicFrame in magickImageCollection) { var frame = new ImageFrame <TPixel>(configuration, magicFrame.Width, magicFrame.Height); framesList.Add(frame); MemoryGroup <TPixel> framePixels = frame.PixelBuffer.FastMemoryGroup; using IUnsafePixelCollection <ushort> pixels = magicFrame.GetPixelsUnsafe(); if (magicFrame.Depth == 8 || magicFrame.Depth == 1) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); FromRgba32Bytes(configuration, data, framePixels); } else if (magicFrame.Depth == 16) { ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); FromRgba64Bytes(configuration, bytes, framePixels); } else { throw new InvalidOperationException(); } } return(new Image <TPixel>(configuration, new ImageMetadata(), framesList)); }
public Image <TPixel> Decode <TPixel>(Configuration configuration, Stream stream) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel <TPixel> { var bmpReadDefines = new BmpReadDefines { IgnoreFileSize = !this.validate }; var settings = new MagickReadSettings(); settings.SetDefines(bmpReadDefines); using var magickImage = new MagickImage(stream, settings); var result = new Image <TPixel>(configuration, magickImage.Width, magickImage.Height); MemoryGroup <TPixel> resultPixels = result.GetRootFramePixelBuffer().FastMemoryGroup; using (IUnsafePixelCollection <ushort> pixels = magickImage.GetPixelsUnsafe()) { if (magickImage.Depth == 8) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); FromRgba32Bytes(configuration, data, resultPixels); } else if (magickImage.Depth == 16) { ushort[] data = pixels.ToShortArray(PixelMapping.RGBA); Span <byte> bytes = MemoryMarshal.Cast <ushort, byte>(data.AsSpan()); FromRgba64Bytes(configuration, bytes, resultPixels); } else { throw new InvalidOperationException(); } } return(result); }