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); } }
/// <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; } } } }
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"); }
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(); }
/// <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); } }); } }
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)); }
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)); }
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); }
public void UseCase_123456789() { var buffer = Encoding.UTF8.GetBytes("123456789"); var crc = FeigChecksum.Calculate(BufferSpan.From(buffer)); Check.That(crc).IsEqualTo(0x6F91); }
/// <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(); } } }
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]); }
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); }
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]);
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 }"); }
/// <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; } }
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); } } }
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); } }
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); } }
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()); }
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); } }
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]); } }
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); } }
/// <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); }); } }
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); } }
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); }
private async Task SendAsyncAwaited(BufferSpan span, bool endOfMessage) { await ReadyToSend; var flushSends = endOfMessage || MaxOutstandingSendsReached; Send(GetSegmentFromSpan(span), flushSends); if (flushSends && !endOfMessage) { await ReadyToSend; } }
/// <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); } }
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); } }
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); }