public static Image <TPixelTo> LoadPixels <TPixelFrom, TPixelTo>( ReadOnlySpan <TPixelFrom> pixels, Rectangle sourceRectangle, int?pixelStride = null) where TPixelFrom : unmanaged, IPixel where TPixelTo : unmanaged, IPixel <TPixelTo> { if (pixels.IsEmpty) { throw new ArgumentEmptyException(nameof(pixels)); } ImagingArgumentGuard.AssertNonEmptyRectangle(sourceRectangle, nameof(sourceRectangle)); var image = new Image <TPixelTo>(sourceRectangle.Size); try { LoadPixels(pixels, image, sourceRectangle, pixelStride); } catch { image.Dispose(); throw; } return(image); }
public static void LoadPixels <TPixelFrom, TPixelTo>( IReadOnlyPixelRows pixels, IPixelBuffer <TPixelTo> destination, Rectangle?sourceRectangle = null) where TPixelFrom : unmanaged, IPixel where TPixelTo : unmanaged, IPixel <TPixelTo> { if (pixels == null) { throw new ArgumentNullException(nameof(pixels)); } if (destination == null) { throw new ArgumentNullException(nameof(destination)); } var rect = sourceRectangle ?? pixels.GetBounds(); ImagingArgumentGuard.AssertNonEmptyRectangle(rect, nameof(sourceRectangle)); Span <byte> rowByteBuffer = stackalloc byte[4096]; var rowBuffer = MemoryMarshal.Cast <byte, TPixelFrom>(rowByteBuffer); for (int y = 0; y < rect.Height; y++) { var dstRow = destination.GetPixelRowSpan(y); int offsetX = 0; do { int left = rect.Width - offsetX; int count = Math.Min(rowBuffer.Length, left); var slice = rowBuffer.Slice(0, count); pixels.GetPixelByteRow(rect.X + offsetX, rect.Y + y, MemoryMarshal.AsBytes(slice)); ConvertPixels(slice, dstRow); dstRow = dstRow[count..];
// TODO: move these out of Image class // AND ALSO make them into extenions for Image class public static void LoadPixels <TPixelFrom, TPixelTo>( ReadOnlySpan <TPixelFrom> pixels, IPixelBuffer <TPixelTo> destination, Rectangle sourceRectangle, int?pixelStride = null) where TPixelFrom : unmanaged, IPixel where TPixelTo : unmanaged, IPixel <TPixelTo> { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } ImagingArgumentGuard.AssertNonEmptyRectangle(sourceRectangle, nameof(sourceRectangle)); int srcStride = pixelStride ?? sourceRectangle.Width; for (int y = 0; y < sourceRectangle.Height; y++) { var srcRow = pixels.Slice(sourceRectangle.X + (sourceRectangle.Y + y) * srcStride, srcStride); var dstRow = destination.GetPixelRow(y); ConvertPixels(srcRow, dstRow); } }