public static unsafe void CopyPixels <TSrcPixel, TDstPixel>(SpanBitmap <TSrcPixel> src, Image <TDstPixel> dst)
            where TSrcPixel : unmanaged
            where TDstPixel : unmanaged, IPixel <TDstPixel>
        {
            if (dst == null)
            {
                throw new ArgumentNullException(nameof(dst));
            }
            if (src.Width != dst.Width || src.Height != dst.Height)
            {
                throw new ArgumentException("dimensions mismatch", nameof(dst));
            }
            if (sizeof(TSrcPixel) != sizeof(TDstPixel))
            {
                throw new ArgumentException("Pixel size mismatch", typeof(TDstPixel).Name);
            }

            src = src.AsReadOnly();

            for (int y = 0; y < dst.Height; y++)
            {
                var srcRow = src.GetScanlinePixels(y);
                var dstRow = dst.DangerousGetPixelRowMemory(y).Span;

                MEMORYMARSHALL
                .Cast <TSrcPixel, TDstPixel>(srcRow)
                .CopyTo(dstRow);
            }
        }
        public static TResult ReadAsSpanBitmap <TSelfPixel, TOtherPixel, TResult>(Image <TSelfPixel> self, SpanBitmap <TOtherPixel> other, SpanBitmap <TOtherPixel> .Function2 <TResult> function)
            where TSelfPixel : unmanaged, IPixel <TSelfPixel>
            where TOtherPixel : unmanaged
        {
            if (self == null)
            {
                throw new ArgumentNullException(nameof(self));
            }

            if (!TryGetExactPixelFormat <TSelfPixel>(out var otherFmt))
            {
                throw new NotImplementedException($"{typeof(TSelfPixel)}");
            }

            if (self.DangerousTryGetSinglePixelMemory(out Memory <TSelfPixel> selfMem))
            {
                var selfBytes = MEMORYMARSHALL.Cast <TSelfPixel, Byte>(selfMem.Span);
                var selfBmp   = new SpanBitmap <TOtherPixel>(selfBytes, self.Width, self.Height, otherFmt);

                return(function(selfBmp.AsReadOnly(), other));
            }
            else
            {
                var tempBmp = ImageSharpToolkit
                              .ToMemoryBitmap <TOtherPixel>(self)
                              .AsSpanBitmap()
                              .AsReadOnly();

                return(function(tempBmp, other));
            }
        }
Beispiel #3
0
        public static void Transfer(SpanBitmap src, GDIBITMAP dst, SpanBitmap.Action2 action)
        {
            src = src.AsReadOnly();

            var rect = new Rectangle(0, 0, dst.Width, dst.Height);

            GDIPTR dstBits = null;

            try
            {
                dstBits = dst.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, dst.PixelFormat);

                var dstSpan = dstBits
                              .AsPointerBitmapDangerous()
                              .AsSpanBitmap();

                action(src, dstSpan);
            }
            finally
            {
                if (dstBits != null)
                {
                    dst.UnlockBits(dstBits);
                }
            }
        }
        public static void CopyPixels <TDstPixel>(SpanBitmap src, Image <TDstPixel> dst)
            where TDstPixel : unmanaged, IPixel <TDstPixel>
        {
            src = src.AsReadOnly();

            for (int y = 0; y < dst.Height; y++)
            {
                var srcRow = src.GetScanlineBytes(y);
                var dstRow = dst.DangerousGetPixelRowMemory(y).Span;

                MEMORYMARSHALL
                .Cast <Byte, TDstPixel>(srcRow)
                .CopyTo(dstRow);
            }
        }
        public static unsafe void WriteAsSpanBitmap <TSelfPixel, TOtherPixel>(Image <TSelfPixel> self, SpanBitmap <TOtherPixel> other, SpanBitmap <TOtherPixel> .Action2 action)
            where TSelfPixel : unmanaged, IPixel <TSelfPixel>
            where TOtherPixel : unmanaged
        {
            if (self == null)
            {
                throw new ArgumentNullException(nameof(self));
            }

            if (sizeof(TSelfPixel) != sizeof(TOtherPixel))
            {
                throw new ArgumentException("pixel size mismatch", typeof(TOtherPixel).Name);
            }

            other = other.AsReadOnly();

            if (!TryGetExactPixelFormat <TSelfPixel>(out var otherFmt))
            {
                otherFmt = PixelFormat.TryIdentifyFormat <TOtherPixel>();
            }

            if (self.DangerousTryGetSinglePixelMemory(out Memory <TSelfPixel> selfMem))
            {
                var selfBytes = MEMORYMARSHALL.Cast <TSelfPixel, Byte>(selfMem.Span);
                var selfBmp   = new SpanBitmap <TOtherPixel>(selfBytes, self.Width, self.Height, otherFmt);
                action(selfBmp, other);
            }
            else
            {
                var tempBmp = new MemoryBitmap <TOtherPixel>(self.Width, self.Height, otherFmt);

                CopyPixels(self, tempBmp);
                action(tempBmp, other);
                CopyPixels(tempBmp, self);
            }
        }