private int DrawVisibleLine(Point start, Point end, byte[] pixelsSource, byte[] pixelsSourceToWrite, int stride) { var points = Math2D.line(start, end); int pixel = 0; //if we start in a fixture we can see all the fixture, move on till we get out of it. while (pixel < points.Count && Math2D.GetKindOfTerrain(pixelsSource, points[pixel]) == groundColor.fixture) { Math2D.SetPixeDarkRed(pixelsSourceToWrite, points[pixel]); pixel++; } //now we are for sure in the open. Keep on till the next fixture while (pixel < points.Count && Math2D.GetKindOfTerrain(pixelsSource, points[pixel]) != groundColor.fixture) { var obj = Math2D.GetKindOfTerrain(pixelsSource, points[pixel]); if (obj == groundColor.open) { Math2D.SetPixelRed(pixelsSourceToWrite, points[pixel]); } else { Math2D.SetPixelRGB(pixelsSourceToWrite, points[pixel], 220, 160, 100); } pixel++; } //we are in the last fixture we will see. Keep on untill we reach it's limit while (pixel < points.Count && Math2D.GetKindOfTerrain(pixelsSource, points[pixel]) == groundColor.fixture) { Math2D.SetPixeDarkRed(pixelsSourceToWrite, points[pixel]); pixel++; } return(pixel); }
private void CompresImageToColor(byte r, byte g, byte b, bool BackGroundWhite = false) { RenderTargetBitmap rtb = new RenderTargetBitmap((int)MainCanvas.RenderSize.Width, (int)MainCanvas.RenderSize.Height, 96d, 96d, System.Windows.Media.PixelFormats.Default); rtb.Render(MainCanvas); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(rtb)); var temporalBitmap = new BitmapImage(); using (var stream = new MemoryStream()) { bitmapEncoder.Save(stream); stream.Seek(0, SeekOrigin.Begin); temporalBitmap.BeginInit(); temporalBitmap.CacheOption = BitmapCacheOption.OnLoad; temporalBitmap.StreamSource = stream; temporalBitmap.EndInit(); } WriteableBitmap temporalSource; if (Fixtures.compressedBMimage == null) { Fixtures.compressedBMimage = new WriteableBitmap(temporalBitmap); temporalSource = Fixtures.compressedBMimage; } else { temporalSource = new WriteableBitmap(temporalBitmap); } int width = (int)temporalSource.Width; int height = (int)temporalSource.Height; Math2D.stride = temporalSource.PixelWidth * 4; int size = temporalSource.PixelHeight * Math2D.stride; byte[] pixelsSource = new byte[size]; temporalSource.CopyPixels(pixelsSource, Math2D.stride, 0); byte[] pixelsToWrite = new byte[size]; if (Fixtures.compressedBMimage != null) { Fixtures.compressedBMimage.CopyPixels(pixelsToWrite, Math2D.stride, 0); } for (int y = 0; y < height; y++) { for (int x = 0; x < width; ++x) { if (Math2D.IsPixelDifferntThanWhie(pixelsSource, new Point(x, y))) { Math2D.SetPixelRGB(pixelsToWrite, new Point(x, y), r, g, b); } else { if (BackGroundWhite) { Math2D.SetPixelRGB(pixelsToWrite, new Point(x, y), 255, 255, 255); } } } } Fixtures.compressedBMimage.WritePixels(new Int32Rect(0, 0, width, height), pixelsToWrite, width * 4, 0); CompressedImage.Source = Fixtures.compressedBMimage; }