コード例 #1
0
        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);
        }
コード例 #2
0
        public static Image <TPixel> WrapMemory <TPixel>(
            Memory <byte> memory, Size size, int?byteStride = null)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            ArgumentGuard.AssertDimensionsGreaterThanZero(size, nameof(size));
            ImagingArgumentGuard.AssertContigousLargeEnough(memory.Length, size.Area, nameof(memory));

            int stride = byteStride ?? (size.Width * Unsafe.SizeOf <TPixel>());

            AssertValidStride <TPixel>(size, memory.Length, stride, nameof(byteStride));
            AssertValidMemory(size, memory.Length, stride, nameof(memory));

            var buffer = new Image <TPixel> .PixelBuffer(memory, stride);

            return(new Image <TPixel>(buffer, size));
        }
コード例 #3
0
        public static Image <TPixel> WrapMemory <TPixel>(
            IMemory memory, Size size, bool leaveOpen = false, int?byteStride = null)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            if (memory == null)
            {
                throw new ArgumentNullException(nameof(memory));
            }

            ArgumentGuard.AssertDimensionsGreaterThanZero(size, nameof(size));
            ImagingArgumentGuard.AssertContigousLargeEnough(memory.Length, size.Area, nameof(memory));

            int stride = byteStride ?? (size.Width * Unsafe.SizeOf <TPixel>());

            AssertValidStride <TPixel>(size, memory.Length * memory.ElementSize, stride, nameof(byteStride));
            AssertValidMemory(size, memory.Length * memory.ElementSize, stride, nameof(memory));

            var buffer = new Image <TPixel> .PixelBuffer(memory, stride, leaveOpen);

            return(new Image <TPixel>(buffer, size));
        }
コード例 #4
0
        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..];
コード例 #5
0
        // 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);
            }
        }