public WriteableBitmap GetBitmap(int width, int height, double dpiX, double dpiY) { var fromRGB = Conversions.FromColor(_fromColor); var toRGB = Conversions.FromColor(_toColor); // 4 channels (blue, green, red, alpha). Order of channels is important! int channels = 4; byte[] pixels = new byte[width * height * channels]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { var rgb = Vector3.Lerp(fromRGB, toRGB, i / (float)width); var color = Conversions.FromRGB(rgb, gammaCorrection: j < (height / 2)); color.Clamp(); var pos = (j * width * channels) + (i * channels); // blue channel pixels[pos + 0] = color.B; // green channel pixels[pos + 1] = color.G; // red channel pixels[pos + 2] = color.R; // alpha channel pixels[pos + 3] = byte.MaxValue; } } var bitmap = new WriteableBitmap(width, height, dpiX, dpiY, PixelFormats.Bgra32, BitmapPalettes.Halftone256); bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * channels, 0); return(bitmap); }
public ImageSource GetImage() { //** clear buffers Array.Clear(_rgbArray, 0, _rgbArray.Length); for (int x = 0; x < _screenWidth; x++) { for (int y = 0; y < _screenHeight; y++) { _zBufferArray[x, y] = float.PositiveInfinity; } } if (_useDeferredRenderer) { //** first pass (Z-Prepass) for (int i = 0; i < _triangles.Length; i++) { var triangle = _triangles[i]; if (!triangle.IsBackfacing) { for (int x = (int)Math.Min(0, Math.Max(triangle.MinScreenX, 0)); x < (int)Math.Min(Math.Max(triangle.MaxScreenX, 0), _screenWidth); x++) { for (int y = (int)Math.Min(0, Math.Max(triangle.MinScreenY, 0)); y < (int)Math.Min(Math.Max(triangle.MaxScreenY, 0), _screenHeight); y++) { var z = triangle.CalcZ(x, y); if (!float.IsInfinity(z) && !float.IsNaN(z) && z < _zBufferArray[x, y]) { _zBufferArray[x, y] = z; } } } } } //** second pass for (int i = 0; i < _triangles.Length; i++) { var triangle = _triangles[i]; if (!triangle.IsBackfacing) { for (int x = (int)Math.Min(0, Math.Max(triangle.MinScreenX, 0)); x < (int)Math.Min(Math.Max(triangle.MaxScreenX, 0), _screenWidth); x++) { for (int y = (int)Math.Min(0, Math.Max(triangle.MinScreenY, 0)); y < (int)Math.Min(Math.Max(triangle.MaxScreenY, 0), _screenHeight); y++) { float z = _zBufferArray[x, y]; if (_visualizeZBuffer) { var zcolor = (int)((z - ZBUFFER_VISUALIZE_MIN) / (ZBUFFER_VISUALIZE_MAX - ZBUFFER_VISUALIZE_MIN) * 255) * new Vector3(1f / 255, 1f / 255, 1f / 255); _rgbArray[x, y] = zcolor; } else { var rgb = Vector3.Zero; if (triangle.CalcColorDeferred(x, y, _lightSources, z, out rgb)) { _rgbArray[x, y] = rgb; } } } } } } } else { for (int i = 0; i < _triangles.Length; i++) { var triangle = _triangles[i]; if (!triangle.IsBackfacing) { for (int x = (int)Math.Min(0, Math.Max(triangle.MinScreenX, 0)); x < (int)Math.Min(Math.Max(triangle.MaxScreenX, 0), _screenWidth); x++) { for (int y = (int)Math.Min(0, Math.Max(triangle.MinScreenY, 0)); y < (int)Math.Min(Math.Max(triangle.MaxScreenY, 0), _screenHeight); y++) { float z; Vector3 rgb; if (triangle.CalcColor(x, y, _lightSources, out z, out rgb) && !float.IsInfinity(z) && !float.IsNaN(z)) { if (z < _zBufferArray[x, y]) { _zBufferArray[x, y] = z; if (_visualizeZBuffer) { var zcolor = (int)((z - ZBUFFER_VISUALIZE_MIN) / (ZBUFFER_VISUALIZE_MAX - ZBUFFER_VISUALIZE_MIN) * 255) * new Vector3(1f / 255, 1f / 255, 1f / 255); _rgbArray[x, y] = zcolor; } else { _rgbArray[x, y] = rgb; } } } } } } } } //** buffer to image for (int x = 0; x < _screenWidth; x++) { for (int y = 0; y < _screenHeight; y++) { var rgb = _rgbArray[x, y]; var c = Conversions.FromRGB(rgb, _gammaCorrect); _bitmap.Set(x, y, c); } } return(_bitmap.GetImageSource()); }
public string GetImageFileName(int width, int height, double dpiX, double dpiY, CancellationToken cancellationToken, string settingsSummary, string exportDirectory) { var sw = Stopwatch.StartNew(); OutputLogEveryXPixel = (width * height / 100); if (AccelerationStructure) { _accelerationStructure = BVHNode.BuildTopDown(_spheres, _logger); } var bitmap = new BitmapImage(width, height, dpiX, dpiY); var divideX = width / (float)2; var alignX = 1 - divideX; var divideY = height / (float)2; var alignY = 1 - divideY; int workDone = 0; int totalWork = width * height; if (Parallelize) { var options = new ParallelOptions() { CancellationToken = cancellationToken, MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.For(0, width, options, i => { for (int j = 0; j < height; j++) { if (cancellationToken.IsCancellationRequested) { break; } if (AntiAliasing) { Vector3 result = Vector3.Zero; for (int k = 0; k < AntiAliasingSampleSize; k++) { var dx = (float)_random.NextGaussian(0d, 0.5d); var dy = (float)_random.NextGaussian(0d, 0.5d); var x = (i + alignX + dx) / divideX; var y = ((height - j) + alignY + dy) / divideY; var rgb = GetColor(x, y); result += rgb; } var avg_rgb = result * (1f / AntiAliasingSampleSize); var c = Conversions.FromRGB(avg_rgb, GammaCorrect); bitmap.Set(i, j, c); } else { var x = (i + alignX) / divideX; var y = ((height - j) + alignY) / divideY; var rgb = GetColor(x, y); var c = Conversions.FromRGB(rgb, GammaCorrect); bitmap.Set(i, j, c); } var value = Interlocked.Increment(ref workDone); if (value % OutputLogEveryXPixel == 0) { var progress = (float)value / totalWork; WriteOutput($"{(progress * 100):F3}% progress. Running {sw.Elapsed}. Remaining {TimeSpan.FromMilliseconds(sw.Elapsed.TotalMilliseconds / progress * (1f - progress))}."); } } }); } else { for (int i = 0; i < width; i++) { if (cancellationToken.IsCancellationRequested) { break; } for (int j = 0; j < height; j++) { if (cancellationToken.IsCancellationRequested) { break; } if (AntiAliasing) { Vector3 result = Vector3.Zero; for (int k = 0; k < AntiAliasingSampleSize; k++) { var x = (i + alignX + (float)_random.NextGaussian(0d, 0.5d)) / divideX; var y = ((height - j) + alignY + (float)_random.NextGaussian(0d, 0.5d)) / divideY; var rgb = GetColor(x, y); result += rgb; } var avg_rgb = result * (1f / AntiAliasingSampleSize); var c = Conversions.FromRGB(avg_rgb, GammaCorrect); bitmap.Set(i, j, c); } else { // Question: Why is it mirrored? var x = (i + alignX) / divideX; var y = ((height - j) + alignY) / divideY; var rgb = GetColor(x, y); var c = Conversions.FromRGB(rgb, GammaCorrect); bitmap.Set(i, j, c); } ++workDone; if (workDone % OutputLogEveryXPixel == 0) { var progress = (float)workDone / totalWork; WriteOutput($"{(progress * 100):F3}% progress. Running {sw.Elapsed}. Remaining {TimeSpan.FromMilliseconds(sw.Elapsed.TotalMilliseconds / progress * (1f - progress))}."); } } } } if (cancellationToken.IsCancellationRequested) { WriteOutput("Operation canceled by user."); return(null); } var imageSource = bitmap.GetImageSource(); sw.Stop(); return(SaveImage(imageSource, sw.Elapsed, settingsSummary, exportDirectory)); }