/// <summary> /// Debug function that finds the bodies in the image with the stencil router, and /// assigns each of them a random color in the preview image. /// </summary> private void ColorImageBodies() { List <Body> bodies = StencilRouter.FindBodies(ImageToEtch); using (Avalonia.Platform.ILockedFramebuffer buffer = ImageToEtch.Bitmap.Lock()) { Random rng = new Random(); foreach (Body body in bodies) { byte randomBlue = (byte)rng.Next(1, 255); byte randomGreen = (byte)rng.Next(1, 255); byte randomRed = (byte)rng.Next(1, 255); int pixelValue; unchecked { pixelValue = (int)0xFF000000; pixelValue |= (randomRed << 16) | (randomGreen << 8) | randomBlue; } foreach (Point point in body.Points) { IntPtr rowOffsetAddress = buffer.Address + (point.Y * buffer.Size.Width * 4); IntPtr pixelAddress = rowOffsetAddress + (point.X * 4); Marshal.WriteInt32(pixelAddress, pixelValue); } } } Image preview = this.FindControl <Image>("Preview"); preview.InvalidateVisual(); }
/// <summary> /// Debug function that draws the etch paths onto the preview image /// </summary> private void DrawRoute() { List <Path> paths = Route.EtchPaths; using (Avalonia.Platform.ILockedFramebuffer buffer = ImageToEtch.Bitmap.Lock()) { foreach (Path outline in paths) { double numberOfPoints = outline.Points.Count; double halfMark = numberOfPoints / 2.0; for (int i = 0; i < numberOfPoints; i++) { Point point = outline.Points[i]; // Green stays at 255 until the halfway mark, then linearly decreases to 0 double greenScale = Math.Max(0, i - halfMark); byte green = (byte)Math.Round((halfMark - greenScale) / halfMark * 255.0); // Red starts at 0 and linearly increases to 255 at the halfway mark byte red = (byte)Math.Round(Math.Min(1.0, i / halfMark) * 255.0); int pixelValue; unchecked { pixelValue = (int)0xFF000000; pixelValue |= (red << 16) | (green << 8); } IntPtr rowOffsetAddress = buffer.Address + (point.Y * buffer.Size.Width * 4); IntPtr pixelAddress = rowOffsetAddress + (point.X * 4); Marshal.WriteInt32(pixelAddress, pixelValue); } } } Image preview = this.FindControl <Image>("Preview"); preview.InvalidateVisual(); }