コード例 #1
0
        private void BulkVectorConvert <TPixel>(BufferSpan <TPixel> destination, BufferSpan <TPixel> background, BufferSpan <TPixel> source, BufferSpan <float> amount)
            where TPixel : struct, IPixel <TPixel>
        {
            Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length));
            Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length));
            Guard.MustBeGreaterThanOrEqualTo(amount.Length, destination.Length, nameof(amount.Length));

            using (Buffer <Vector4> buffer = new Buffer <Vector4>(destination.Length * 3))
            {
                BufferSpan <Vector4> destinationSpan = buffer.Slice(0, destination.Length);
                BufferSpan <Vector4> backgroundSpan  = buffer.Slice(destination.Length, destination.Length);
                BufferSpan <Vector4> sourceSpan      = buffer.Slice(destination.Length * 2, destination.Length);

                PixelOperations <TPixel> .Instance.ToVector4(background, backgroundSpan, destination.Length);

                PixelOperations <TPixel> .Instance.ToVector4(source, sourceSpan, destination.Length);

                for (int i = 0; i < destination.Length; i++)
                {
                    destinationSpan[i] = PorterDuffFunctions.NormalBlendFunction(backgroundSpan[i], sourceSpan[i], amount[i]);
                }

                PixelOperations <TPixel> .Instance.PackFromVector4(destinationSpan, destination, destination.Length);
            }
        }
コード例 #2
0
            /// <inheritdoc />
            internal override void Apply(float[] scanlineBuffer, int scanlineWidth, int offset, int x, int y)
            {
                Guard.MustBeGreaterThanOrEqualTo(scanlineBuffer.Length, offset + scanlineWidth, nameof(scanlineWidth));

                using (Buffer <float> buffer = new Buffer <float>(scanlineBuffer))
                {
                    BufferSpan <float> slice = buffer.Slice(offset);

                    for (int xPos = 0; xPos < scanlineWidth; xPos++)
                    {
                        int targetX = xPos + x;
                        int targetY = y;

                        float opacity = slice[xPos];
                        if (opacity > Constants.Epsilon)
                        {
                            Vector4 backgroundVector = this.Target[targetX, targetY].ToVector4();
                            Vector4 sourceVector     = this.colorVector;

                            Vector4 finalColor = Vector4BlendTransforms.PremultipliedLerp(backgroundVector, sourceVector, opacity);

                            TColor packed = default(TColor);
                            packed.PackFromVector4(finalColor);
                            this.Target[targetX, targetY] = packed;
                        }
                    }
                }
            }
コード例 #3
0
        public void StandardFrame()
        {
            // act
            var req = new FeigRequest {
                Address = 0x12, Command = FeigCommand.ReadConfiguration, Data = BufferSpan.From(0x07),
            };

            // assert
            Check.That(req.Address).IsEqualTo(0x12);
            Check.That(req.Command).IsEqualTo(FeigCommand.ReadConfiguration);
            Check.That(req.Data.Buffer).ContainsExactly(0x07);

            // act
            var frame = req.ToBufferSpan();

            // assert
            Check.That(frame.ToArray())
            .ContainsExactly(
                0x06,
                0x12,
                0x80,
                0x07,
                0xe5,
                0x80
                );

            Check.That(req.ToString()).IsEqualTo("Address: 18, Command: ReadConfiguration, Data: 07");
        }
コード例 #4
0
            public void AdvancedResponse_GetSoftwareVersion_FrameError()
            {
                // act
                var data = new Byte[] {
                    /*0x02*/
                    0xFF,
                    0x00,
                    0x0F,
                    0x00,
                    0x65,
                    0x00,
                    0x03,
                    0x03,
                    0x00,
                    0x44,
                    0x53,
                    0x0D,
                    0x30,
                    0x74,
                    0x68,
                };

                var span = BufferSpan.From(data, 0, data.Length);

                var result = FeigResponse.TryParse(span, FeigProtocol.Advanced);

                // assert
                Check.That(result.Status).IsEqualTo(FeigParseStatus.FrameError);
                Check.That(result.Response).IsNull();
            }
コード例 #5
0
ファイル: GlowProcessor.cs プロジェクト: jimmymain/ImageSharp
        /// <inheritdoc/>
        protected override void OnApply(ImageBase <TPixel> source, Rectangle sourceRectangle)
        {
            int     startY      = sourceRectangle.Y;
            int     endY        = sourceRectangle.Bottom;
            int     startX      = sourceRectangle.X;
            int     endX        = sourceRectangle.Right;
            TPixel  glowColor   = this.GlowColor;
            Vector2 centre      = Rectangle.Center(sourceRectangle).ToVector2();
            float   maxDistance = this.Radius > 0 ? MathF.Min(this.Radius, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F;

            // Align start/end positions.
            int minX = Math.Max(0, startX);
            int maxX = Math.Min(source.Width, endX);
            int minY = Math.Max(0, startY);
            int maxY = Math.Min(source.Height, endY);

            // Reset offset if necessary.
            if (minX > 0)
            {
                startX = 0;
            }

            if (minY > 0)
            {
                startY = 0;
            }

            int width = maxX - minX;

            using (Buffer <TPixel> rowColors = new Buffer <TPixel>(width))
                using (PixelAccessor <TPixel> sourcePixels = source.Lock())
                {
                    for (int i = 0; i < width; i++)
                    {
                        rowColors[i] = glowColor;
                    }

                    Parallel.For(
                        minY,
                        maxY,
                        this.ParallelOptions,
                        y =>
                    {
                        using (Buffer <float> amounts = new Buffer <float>(width))
                        {
                            int offsetY = y - startY;
                            int offsetX = minX - startX;
                            for (int i = 0; i < width; i++)
                            {
                                float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY));
                                amounts[i]     = (this.options.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1);
                            }

                            BufferSpan <TPixel> destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width);

                            this.blender.Blend(destination, destination, rowColors, amounts);
                        }
                    });
                }
        }
コード例 #6
0
        public override Collection <VSTF.TextBounds> GetNormalizedTextBounds(HexBufferSpan bufferPosition, HexSpanSelectionFlags flags)
        {
            if (!IsValid)
            {
                throw new ObjectDisposedException(nameof(HexFormattedLineImpl));
            }
            var pos  = BufferSpan.Intersection(bufferPosition);
            var list = new List <VSTF.TextBounds>();

            if (pos is null)
            {
                return(new Collection <VSTF.TextBounds>(list));
            }

            foreach (var info in bufferLine.GetSpans(pos.Value, flags))
            {
                var valuesSpan = TryGetNormalizedTextBounds(info.TextSpan);
                if (!(valuesSpan is null))
                {
                    list.Add(valuesSpan.Value);
                }
            }

            return(new Collection <VSTF.TextBounds>(list));
        }
コード例 #7
0
        private BufferSpan _ToAdvancedProtocolFrame()
        {
            var frameLength = 7 + Data.Count;
            var frame       = new Byte[frameLength];

            frame[0] = 0x02;
            frame[1] = (Byte)(frameLength >> 8);
            frame[2] = (Byte)(frameLength & 0xff);
            frame[3] = Address;
            frame[4] = (Byte)Command;

            Buffer.BlockCopy(
                Data.Buffer,
                Data.Offset,
                frame,
                5,
                Data.Count
                );

            var crc = FeigChecksum.Calculate(BufferSpan.From(frame, 0, frameLength - 2));

            frame[frameLength - 2] = (Byte)(crc & 0xff);
            frame[frameLength - 1] = (Byte)(crc >> 8);

            return(BufferSpan.From(frame, frameLength));
        }
コード例 #8
0
        public void MultiplyFunction_Blender_Bulk <TPixel>(TestPixel <TPixel> back, TestPixel <TPixel> source, float amount, TestPixel <TPixel> expected)
            where TPixel : struct, IPixel <TPixel>
        {
            BufferSpan <TPixel> dest = new BufferSpan <TPixel>(new TPixel[1]);

            new DefaultMultiplyPixelBlender <TPixel>().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount));
            VectorAssert.Equal(expected, dest[0], 2);
        }
コード例 #9
0
        public void UseCase_123456789()
        {
            var buffer = Encoding.UTF8.GetBytes("123456789");

            var crc = FeigChecksum.Calculate(BufferSpan.From(buffer));

            Check.That(crc).IsEqualTo(0x6F91);
        }
コード例 #10
0
        /// <summary>
        /// Gets column spans in column order
        /// </summary>
        /// <param name="span">Buffer span</param>
        /// <param name="flags">Flags</param>
        /// <returns></returns>
        public IEnumerable <TextAndHexSpan> GetSpans(HexBufferSpan span, HexSpanSelectionFlags flags)
        {
            if (span.IsDefault)
            {
                throw new ArgumentException();
            }
            if (span.Buffer != Buffer)
            {
                throw new ArgumentException();
            }

            var overlapSpan = BufferSpan.Overlap(span);

            if (overlapSpan == null)
            {
                yield break;
            }

            foreach (var column in ColumnOrder)
            {
                switch (column)
                {
                case HexColumnType.Offset:
                    if ((flags & HexSpanSelectionFlags.Offset) != 0 && IsOffsetColumnPresent)
                    {
                        if (BufferSpan.Contains(overlapSpan.Value))
                        {
                            yield return(new TextAndHexSpan(GetOffsetSpan(), BufferSpan));
                        }
                    }
                    break;

                case HexColumnType.Values:
                    if ((flags & HexSpanSelectionFlags.Values) != 0)
                    {
                        foreach (var info in GetValuesSpans(overlapSpan.Value, flags))
                        {
                            yield return(info);
                        }
                    }
                    break;

                case HexColumnType.Ascii:
                    if ((flags & HexSpanSelectionFlags.Ascii) != 0)
                    {
                        foreach (var info in GetAsciiSpans(overlapSpan.Value, flags))
                        {
                            yield return(info);
                        }
                    }
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }
        }
コード例 #11
0
            public void Write(int length, int start, int index)
            {
                Foo[]            a    = Foo.CreateArray(length);
                BufferSpan <Foo> span = new BufferSpan <Foo>(a, start);

                span[index] = new Foo(666, 666);

                Assert.Equal(new Foo(666, 666), a[start + index]);
            }
コード例 #12
0
            public void Read(int length, int start, int index)
            {
                Foo[]            a    = Foo.CreateArray(length);
                BufferSpan <Foo> span = new BufferSpan <Foo>(a, start);

                Foo element = span[index];

                Assert.Equal(a[start + index], element);
            }
コード例 #13
0
            public void AsBytes_Read(int length, int start, int index, int byteOffset)
            {
                Foo[]            a    = Foo.CreateArray(length);
                BufferSpan <Foo> span = new BufferSpan <Foo>(a, start);

                BufferSpan <byte> bytes = span.AsBytes();

                byte actual = bytes[index * Unsafe.SizeOf <Foo>() + byteOffset];

                ref byte baseRef  = ref Unsafe.As <Foo, byte>(ref a[0]);
コード例 #14
0
        public void ToString_SingleItems()
        {
            var transponders = new[] {
                new FeigTransponder {
                    TransponderType = FeigTransponderType.ISO14443A, Identifier = BufferSpan.From(0x11, 0x22, 0x33),
                },
            };

            Check.That(FeigTransponder.ToString(transponders)).IsEqualTo("{ Type: ISO14443A, ID: 11-22-33 }");
        }
コード例 #15
0
        /// <summary>
        /// Processes the de-filtered scanline filling the image pixel data
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="defilteredScanline">The de-filtered scanline</param>
        /// <param name="pixels">The image pixels</param>
        private void ProcessDefilteredScanline <TPixel>(byte[] defilteredScanline, PixelAccessor <TPixel> pixels)
            where TPixel : struct, IPixel <TPixel>
        {
            TPixel color = default(TPixel);
            BufferSpan <TPixel> pixelBuffer    = pixels.GetRowSpan(this.currentRow);
            BufferSpan <byte>   scanlineBuffer = new BufferSpan <byte>(defilteredScanline, 1);

            switch (this.PngColorType)
            {
            case PngColorType.Grayscale:
                int    factor       = 255 / ((int)Math.Pow(2, this.header.BitDepth) - 1);
                byte[] newScanline1 = ToArrayByBitsLength(defilteredScanline, this.bytesPerScanline, this.header.BitDepth);
                for (int x = 0; x < this.header.Width; x++)
                {
                    byte intensity = (byte)(newScanline1[x] * factor);
                    color.PackFromBytes(intensity, intensity, intensity, 255);
                    pixels[x, this.currentRow] = color;
                }

                break;

            case PngColorType.GrayscaleWithAlpha:

                for (int x = 0; x < this.header.Width; x++)
                {
                    int offset = 1 + (x * this.bytesPerPixel);

                    byte intensity = defilteredScanline[offset];
                    byte alpha     = defilteredScanline[offset + this.bytesPerSample];

                    color.PackFromBytes(intensity, intensity, intensity, alpha);
                    pixels[x, this.currentRow] = color;
                }

                break;

            case PngColorType.Palette:

                this.ProcessScanlineFromPalette(defilteredScanline, pixels);

                break;

            case PngColorType.Rgb:

                PixelOperations <TPixel> .Instance.PackFromXyzBytes(scanlineBuffer, pixelBuffer, this.header.Width);

                break;

            case PngColorType.RgbWithAlpha:

                PixelOperations <TPixel> .Instance.PackFromXyzwBytes(scanlineBuffer, pixelBuffer, this.header.Width);

                break;
            }
        }
コード例 #16
0
        public void Construction()
        {
            var transponder = new FeigTransponder {
                TransponderType = FeigTransponderType.ISO14443A, Identifier = BufferSpan.From(0x11, 0x22, 0x33),
            };

            Check.That(transponder.TransponderType).IsEqualTo(FeigTransponderType.ISO14443A);
            Check.That(transponder.Identifier.ToArray()).ContainsExactly(0x11, 0x22, 0x33);

            Check.That(transponder.ToString()).IsEqualTo("Type: ISO14443A, ID: 11-22-33");
        }
        public void ReceivedDataIgnored()
        {
            var settingsA = new SerialTransportSettings {
                PortName = "COMA"
            };

            var logger = LoggerFactory.Create(
                builder => {
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddSimpleConsole();
            }
                )
                         .CreateLogger("Test");

            using (var transportA = new DefaultFeigTransport(settingsA, logger))
            {
                var settings = new SerialTransportSettings {
                    PortName  = "COMB",
                    Baud      = 38400,
                    DataBits  = 8,
                    Parity    = Parity.Even,
                    StopBits  = StopBits.One,
                    Handshake = Handshake.None,
                };

                using (var transportB = Transport.Create(settings))
                {
                    transportA.Open();
                    transportB.Open();

                    transportB.Send(
                        BufferSpan.From(
                            0x02,
                            0x00,
                            0x0f,
                            0x00,
                            0x65,
                            0x00,
                            0x03,
                            0x03,
                            0x00,
                            0x44,
                            0x53,
                            0x0d,
                            0x30,
                            0x74,
                            0x69
                            )
                        );

                    Thread.Sleep(2000);
                }
            }
        }
コード例 #18
0
        public void GetRowSpanXY(int width, int height, int x, int y)
        {
            using (PinnedImageBuffer <Foo> buffer = new PinnedImageBuffer <Foo>(width, height))
            {
                BufferSpan <Foo> span = buffer.GetRowSpan(x, y);

                Assert.Equal(width * y + x, span.Start);
                Assert.Equal(width - x, span.Length);
                Assert.Equal(buffer.Pointer + sizeof(Foo) * (width * y + x), span.PointerAtOffset);
            }
        }
コード例 #19
0
        public void CastToSpan(int bufferLength)
        {
            using (PinnedBuffer <Foo> buffer = new PinnedBuffer <Foo>(bufferLength))
            {
                BufferSpan <Foo> span = buffer;

                Assert.Equal(buffer.Array, span.Array);
                Assert.Equal(0, span.Start);
                Assert.Equal(buffer.Pointer, span.PointerAtOffset);
                Assert.Equal(span.Length, bufferLength);
            }
        }
コード例 #20
0
            public void Basic()
            {
                Foo[] array = Foo.CreateArray(3);

                // Act:
                BufferSpan <Foo> span = new BufferSpan <Foo>(array);

                // Assert:
                Assert.Equal(array, span.Array);
                Assert.Equal(3, span.Length);
                Assert.SameRefs(ref array[0], ref span.DangerousGetPinnableReference());
            }
コード例 #21
0
        public void Span()
        {
            using (PinnedBuffer <Foo> buffer = new PinnedBuffer <Foo>(42))
            {
                BufferSpan <Foo> span = buffer.Span;

                Assert.Equal(buffer.Array, span.Array);
                Assert.Equal(0, span.Start);
                Assert.Equal(buffer.Pointer, span.PointerAtOffset);
                Assert.Equal(span.Length, 42);
            }
        }
コード例 #22
0
        private void BulkPixelConvert <TPixel>(BufferSpan <TPixel> destination, BufferSpan <TPixel> background, BufferSpan <TPixel> source, BufferSpan <float> amount)
            where TPixel : struct, IPixel <TPixel>
        {
            Guard.MustBeGreaterThanOrEqualTo(destination.Length, background.Length, nameof(destination));
            Guard.MustBeGreaterThanOrEqualTo(source.Length, background.Length, nameof(destination));
            Guard.MustBeGreaterThanOrEqualTo(amount.Length, background.Length, nameof(destination));

            for (int i = 0; i < destination.Length; i++)
            {
                destination[i] = PorterDuffFunctions <TPixel> .NormalBlendFunction(destination[i], source[i], amount[i]);
            }
        }
コード例 #23
0
            public void WithStartAndLength(int bufferLength, int start, int spanLength)
            {
                using (PinnedBuffer <Foo> buffer = new PinnedBuffer <Foo>(bufferLength))
                {
                    BufferSpan <Foo> span = buffer.Slice(start, spanLength);

                    Assert.Equal(buffer.Array, span.Array);
                    Assert.Equal(start, span.Start);
                    Assert.Equal(buffer.Pointer + start * Unsafe.SizeOf <Foo>(), span.PointerAtOffset);
                    Assert.Equal(span.Length, spanLength);
                }
            }
コード例 #24
0
        /// <inheritdoc/>
        protected override void OnApply(ImageBase <TPixel> source, Rectangle sourceRectangle)
        {
            int startY = sourceRectangle.Y;
            int endY   = sourceRectangle.Bottom;
            int startX = sourceRectangle.X;
            int endX   = sourceRectangle.Right;

            // Align start/end positions.
            int minX = Math.Max(0, startX);
            int maxX = Math.Min(source.Width, endX);
            int minY = Math.Max(0, startY);
            int maxY = Math.Min(source.Height, endY);

            // Reset offset if necessary.
            if (minX > 0)
            {
                startX = 0;
            }

            if (minY > 0)
            {
                startY = 0;
            }

            int width = maxX - minX;

            using (Buffer <TPixel> colors = new Buffer <TPixel>(width))
                using (Buffer <float> amount = new Buffer <float>(width))
                    using (PixelAccessor <TPixel> sourcePixels = source.Lock())
                    {
                        for (int i = 0; i < width; i++)
                        {
                            colors[i] = this.Value;
                            amount[i] = this.options.BlendPercentage;
                        }

                        PixelBlender <TPixel> blender = PixelOperations <TPixel> .Instance.GetPixelBlender(this.options.BlenderMode);

                        Parallel.For(
                            minY,
                            maxY,
                            this.ParallelOptions,
                            y =>
                        {
                            int offsetY = y - startY;

                            BufferSpan <TPixel> destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width);

                            // this switched color & destination in the 2nd and 3rd places because we are applying the target colour under the current one
                            blender.Blend(destination, colors, destination, amount);
                        });
                    }
        }
コード例 #25
0
ファイル: BufferSpanTests.cs プロジェクト: vovgou/ImageSharp
        public void AsBytes()
        {
            Foo[] fooz = { new Foo(1, 2), new Foo(3, 4), new Foo(5, 6) };

            using (PinnedBuffer <Foo> colorBuf = new PinnedBuffer <Foo>(fooz))
            {
                BufferSpan <Foo>  orig    = colorBuf.Slice(1);
                BufferSpan <byte> asBytes = (BufferSpan <byte>)orig;

                Assert.Equal(asBytes.Start, sizeof(Foo));
                Assert.Equal(orig.PointerAtOffset, asBytes.PointerAtOffset);
            }
        }
コード例 #26
0
        public bool TranslateTo(ITextSnapshot visualSnapshot, ITextSnapshot editSnapshot)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(nameof(PhysicalLine));
            }

            var  newSpan    = BufferSpan.TranslateTo(editSnapshot, SpanTrackingMode.EdgeExclusive);
            bool hasChanges = HasChanges(BufferSpan.Snapshot, newSpan.Snapshot);

            BufferSpan = newSpan;
            return(hasChanges);
        }
コード例 #27
0
ファイル: RioTcpConnection.cs プロジェクト: timba/Channels
        private async Task SendAsyncAwaited(BufferSpan span, bool endOfMessage)
        {
            await ReadyToSend;

            var flushSends = endOfMessage || MaxOutstandingSendsReached;

            Send(GetSegmentFromSpan(span), flushSends);

            if (flushSends && !endOfMessage)
            {
                await ReadyToSend;
            }
        }
コード例 #28
0
            /// <inheritdoc />
            internal override void Apply(BufferSpan <float> scanline, int x, int y)
            {
                BufferSpan <TPixel> destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length);

                using (Buffer <float> amountBuffer = new Buffer <float>(scanline.Length))
                {
                    for (int i = 0; i < scanline.Length; i++)
                    {
                        amountBuffer[i] = scanline[i] * this.Options.BlendPercentage;
                    }

                    this.Blender.Blend(destinationRow, destinationRow, this.Colors, amountBuffer);
                }
            }
コード例 #29
0
ファイル: BufferSpanTests.cs プロジェクト: vovgou/ImageSharp
            public void Basic()
            {
                Foo[] array = Foo.CreateArray(3);
                fixed(Foo *p = array)
                {
                    // Act:
                    BufferSpan <Foo> span = new BufferSpan <Foo>(array, p);

                    // Assert:
                    Assert.Equal(array, span.Array);
                    Assert.Equal((IntPtr)p, span.PointerAtOffset);
                    Assert.Equal(3, span.Length);
                }
            }
コード例 #30
0
            public void WithStartAndLength()
            {
                Foo[] array  = Foo.CreateArray(10);
                int   start  = 2;
                int   length = 3;
                // Act:
                BufferSpan <Foo> span = new BufferSpan <Foo>(array, start, length);

                // Assert:
                Assert.Equal(array, span.Array);
                Assert.Equal(start, span.Start);
                Assert.SameRefs(ref array[start], ref span.DangerousGetPinnableReference());
                Assert.Equal(length, span.Length);
            }