Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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));
            }
        }
Exemplo n.º 3
0
        public static unsafe BitmapSampler <TPixel> From(SpanTensor2 <TPixel> tensor, ColorEncoding encoding)
        {
            var w = tensor.BitmapSize.Width;
            var h = tensor.BitmapSize.Height;
            var b = MMARSHALL.Cast <TPixel, byte>(tensor.Span);
            var e = encoding;

            return(new BitmapSampler <TPixel>(b, w * sizeof(TPixel), w, h, e));
        }
Exemplo n.º 4
0
        public static void Split(ReadOnlySpan <float> span, out ReadOnlySpan <Vector4> span4, out ReadOnlySpan <float> span1)
        {
            var len = span.Length / 4;

            span4 = MEMMARSHALL.Cast <float, Vector4>(span.Slice(0, len));

            len  *= 4;
            span1 = MEMMARSHALL.Cast <float, float>(span).Slice(len);
        }
Exemplo n.º 5
0
        public static void CopyPixels <TSrcPixel>(Image <TSrcPixel> src, SpanBitmap dst)
            where TSrcPixel : unmanaged, IPixel <TSrcPixel>
        {
            for (int y = 0; y < dst.Height; y++)
            {
                var srcRow = src.DangerousGetPixelRowMemory(y).Span;
                var dstRow = dst.UseScanlineBytes(y);

                MEMORYMARSHALL
                .Cast <TSrcPixel, Byte>(srcRow)
                .CopyTo(dstRow);
            }
        }
Exemplo n.º 6
0
        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);
            }
        }
Exemplo n.º 7
0
        public static unsafe bool TryWrapAsSpanBitmap <TSrcPixel, TDstPixel>(Image <TSrcPixel> src, SpanBitmap <TDstPixel> .Action1 action)
            where TSrcPixel : unmanaged, IPixel <TSrcPixel>
            where TDstPixel : unmanaged
        {
            if (src == null)
            {
                throw new ArgumentNullException(nameof(src));
            }

            if (TryGetExactPixelFormat <TSrcPixel>(out var pfmt))
            {
                if (src.DangerousTryGetSinglePixelMemory(out Memory <TSrcPixel> srcSpan))
                {
                    var span = MEMORYMARSHALL.Cast <TSrcPixel, Byte>(srcSpan.Span);

                    var bmp = new SpanBitmap <TDstPixel>(span, src.Width, src.Height, pfmt);

                    action(bmp);
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 8
0
        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);
            }
        }
Exemplo n.º 9
0
        public static ObjectType FromRawData(Span <uint> values)
        {
            var wordSpan = MemoryMarshal.Cast <uint, ushort>(values);

            return(new ObjectType(wordSpan[0], wordSpan[1] != 0));
        }
Exemplo n.º 10
0
        public sealed override int Read()
        {
            var result = default(char);

            return(Read(MemoryMarshal.CreateSpan(ref result, 1)) > 0 ? result : InvalidChar);
        }