public static Image <TPixel> DecodeWithMagick <TPixel>(FileInfo fileInfo) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel <TPixel> { Configuration configuration = Configuration.Default.Clone(); configuration.PreferContiguousImageBuffers = true; using (var magickImage = new MagickImage(fileInfo)) { magickImage.AutoOrient(); var result = new Image <TPixel>(configuration, magickImage.Width, magickImage.Height); Assert.True(result.DangerousTryGetSinglePixelMemory(out Memory <TPixel> resultPixels)); using (IUnsafePixelCollection <ushort> pixels = magickImage.GetPixelsUnsafe()) { byte[] data = pixels.ToByteArray(PixelMapping.RGBA); PixelOperations <TPixel> .Instance.FromRgba32Bytes( configuration, data, resultPixels.Span, resultPixels.Length); } return(result); } }
private static void CopyPixels <TQuantumType>(IMagickImage <TQuantumType> image, IUnsafePixelCollection <TQuantumType> pixels, PixelFormat format, Bitmap bitmap) where TQuantumType : struct { var mapping = GetMapping(format); for (int y = 0; y < image.Height; y++) { var row = new Rectangle(0, y, image.Width, 1); var data = bitmap.LockBits(row, ImageLockMode.WriteOnly, format); var destination = data.Scan0; var bytes = pixels.ToByteArray(0, y, image.Width, 1, mapping); Marshal.Copy(bytes, 0, destination, bytes.Length); bitmap.UnlockBits(data); } }
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 static Image <TPixel> DecodeWithMagick <TPixel>(Configuration configuration, FileInfo fileInfo) where TPixel : unmanaged, ImageSharp.PixelFormats.IPixel <TPixel> { using var magickImage = new MagickImage(fileInfo); magickImage.AutoOrient(); var result = new Image <TPixel>(configuration, magickImage.Width, magickImage.Height); Assert.True(result.TryGetSinglePixelSpan(out Span <TPixel> resultPixels)); using IUnsafePixelCollection <ushort> pixels = magickImage.GetPixelsUnsafe(); byte[] data = pixels.ToByteArray(PixelMapping.RGBA); PixelOperations <TPixel> .Instance.FromRgba32Bytes( configuration, data, resultPixels, resultPixels.Length); return(result); }
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); }