private void drawMandel() // Niet in een paint-event om te voorkomen dat het scherm kort wit wordt voor het tekenen { // Daarbij is Graphics van PaintEventArgs alleen geldig terwijl het paint event draait, CreateGraphics blijft geldig zolang de Control bestaat int numThreads = (int)nmThreads.Value; // Aantal threads ophalen uit invoer Mandelbrot.scale = double.Parse(tbScale.Text); // Schaal ophalen uit textbox Mandelbrot.maxIterations = int.Parse(tbIterations.Text); // Max. iteraties Mandelbrot.mandelOffset[0] = double.Parse(tbCoordX.Text); // X "coordinaat" Mandelbrot.mandelOffset[1] = double.Parse(tbCoordY.Text); // Y "coordinaat" Mandelbrot.colours[0] = (int) trackRed.Value; // Kleuren opslaan in array Mandelbrot.colours[1] = (int) trackGreen.Value; Mandelbrot.colours[2] = (int) trackBlue.Value; int[] screenSize = { pnFractal.Width, pnFractal.Height }; int[] halfScreenSize = { pnFractal.Width / 2, pnFractal.Height / 2 }; if (numThreads == 0) // Geen threads, blokkeert UI { Mandelbrot.generateImage(pnFractal.CreateGraphics(), 0, 1, screenSize, halfScreenSize); // graphics, startX, stepSize } else { for (int i = 0; i < numThreads; i++) { int start = i; // Moet in een nieuwe variabele voor generateMandelbrot int stepSize = numThreads; // Idem Thread newThread = new Thread(() => Mandelbrot.generateImage(pnFractal.CreateGraphics(), start, stepSize, screenSize, halfScreenSize)); newThread.Name = i.ToString(); // Naam voor debugging purposes newThread.Start(); // Draaien maar! } } }
private void mandelbrot_CB(object sender, EventArgs e) { int width = pictureBox1.Width; int height = pictureBox1.Height; int iterations = 1000; int[] data = new int[width * height]; Utils.InitWatch(); Mandelbrot.CalcCPU(data, width, height, iterations); // Single thread CPU Utils.PrintElapsedTime("CPU Mandelbrot"); Draw(data, width, height, iterations, Color.Blue); Mandelbrot.Dispose(); Mandelbrot.CompileKernel(false); Utils.InitWatch(); Mandelbrot.CalcGPU(data, width, height, iterations); // ILGPU-CPU-Mode Utils.PrintElapsedTime("ILGPU-CPU Mandelbrot"); Draw(data, width, height, iterations, Color.Black); Mandelbrot.Dispose(); Mandelbrot.CompileKernel(true); Utils.InitWatch(); Mandelbrot.CalcGPU(data, width, height, iterations); // ILGPU-GPU-Mode Utils.PrintElapsedTime("ILGPU-CUDA Mandelbrot"); Draw(data, width, height, iterations, Color.Red); }
public static void generateImage(Graphics graphics, int start, int stepSize, int[] screenSize, int[] halfScreenSize) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Bitmap personalBitmap = new Bitmap(screenSize[0], screenSize[1]); // Eigen bitmap zodat er niet gewacht hoeft te worden op andere threads for (int x = start; x < screenSize[0]; x += stepSize) // X ophogen met stepSize (afhankelijk van het aantal gestartte threads) { for (int y = 0; y < screenSize[1]; y++) // Elke thread rekent altijd een volledige baan uit { double scaledX = (x - halfScreenSize[0]) * scale + mandelOffset[0]; // Schalen naar mandelbrot-coordinaten double scaledY = (y - halfScreenSize[1]) * scale + mandelOffset[1]; int mandelNum = Mandelbrot.calculate(scaledX, scaledY); // Draaien daadwerkelijke berekening personalBitmap.SetPixel(x, y, GetColor(mandelNum)); } } graphics.DrawImage(personalBitmap, 0, 0); stopwatch.Stop(); Console.Out.WriteLine("Done in " + stopwatch.ElapsedMilliseconds); }
private void MakeNew(Region reg) { int h = pictureBox1.Height; int w = pictureBox1.Width; var img = Mandelbrot.MakeImage(reg, _palette, _gradient, h, w); // TODO dispose old image pictureBox1.Image = img; }
public static void Main() { TextWriter tmp = Console.Out; var sw = new StringWriter(); Console.SetOut(sw); var m = new Mandelbrot(); Console.SetOut(tmp); sw.Close(); Console.WriteLine(sw.ToString()); }
private void formClosing_CB(object sender, FormClosingEventArgs e) { Mandelbrot.Dispose(); }
public Form() { InitializeComponent(); Mandelbrot.CompileKernel(false); }
public static void Main(string[] args) { var stopWatch = new Stopwatch(); if (args == null) { throw new ArgumentNullException(nameof(args)); } /* * Notes for making palettes - * We generally want the interior of the set to map to black */ var initialPalette = new[] { Tuple.Create(0.0, Color.FromArgb(255, 0, 7, 100)), Tuple.Create(0.16, Color.FromArgb(255, 32, 107, 203)), Tuple.Create(0.42, Color.FromArgb(255, 237, 255, 255)), Tuple.Create(0.6425, Color.FromArgb(255, 255, 170, 0)), Tuple.Create(0.8575, Color.FromArgb(255, 0, 2, 0)), Tuple.Create(1.0, Color.FromArgb(255, 0, 7, 100)) }; int width = 3840, height = 2160, numColors = 768, maxIterations = 256; var argsList = args.ToList(); var paletteParameterIndex = argsList.FindIndex(x => x.Contains("-p") || x.Contains("--palette")); if (paletteParameterIndex >= 0) { var paletteData = Palette.LoadPaletteConfiguration(args[paletteParameterIndex + 1]); Console.WriteLine($@"Palette successfully loaded from {args[paletteParameterIndex + 1]}"); numColors = paletteData.Item1; initialPalette = paletteData.Item2; } var palette = Palette.GenerateColorPalette(initialPalette, numColors); Console.WriteLine($@"Palette has {numColors} colors"); /* * Note that the way scaleDownFactor is calculated will ensure that * `gradient.PaletteScale` is such that the highest ieration counts will map to Black. * * To change the frequency of colors in the output, * change `gradient.IndexScale` in proportion to the frequency, as necessary. */ var scaleDownFactor = Palette.CalculateScaleDownFactorForLinearMapping(Palette.FindPaletteColorLocation(palette, Color.Black)); var root = 4.0; var dimensionParameterIndex = argsList.FindIndex(x => x.Contains("-s") || x.Contains("--size")); if (dimensionParameterIndex >= 0) { var sizeData = args[dimensionParameterIndex + 1].Split(','); if (int.TryParse(sizeData[0], out width)) { Console.WriteLine($"Width = {width}"); } if (int.TryParse(sizeData[1], out height)) { Console.WriteLine($"Height = {height}"); } } var iterationParameterIndex = argsList.FindIndex(x => x.Contains("-i") || x.Contains("--iterations")); if (iterationParameterIndex >= 0) { if (int.TryParse(args[iterationParameterIndex + 1], out maxIterations)) { Console.WriteLine($"Max Iterations = {maxIterations}"); } } var maxIterationColor = Color.Black; var gradient = new Gradient( maxIterationColor, Palette.RecommendedGradientScale(palette.Length, true, scaleDownFactor), 0, 1E10, logIndex: true, rootIndex: false, root: root, minIterations: 1, indexScale: 100, weight: 1.0); var totalPalette = new Color[palette.Length + 1]; totalPalette[0] = maxIterationColor; palette.CopyTo(totalPalette, 1); Palette.SavePaletteAsMspal("./palette.pal", totalPalette); var display = new Display { _bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb) }; var bmp = (Bitmap)display._bmp; var img = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, display._bmp.PixelFormat); var depth = Image.GetPixelFormatSize(img.PixelFormat) / 8; //bytes per pixel //var region = new Region(new Complex(-0.7473121377766069, 0.16276796298735544), new Complex(0.5, 0.5), originAndWidth: true); //var region = new Region(new Complex(-0.1593247826659642, 1.0342115878556377), new Complex(0.0325, 0.0325), originAndWidth: true); //var region = new Region(new Complex(0.27969303810093984, 0.00838423653868096), new Complex(3.27681E-12, 3.27681E-12), originAndWidth: true); var region = new Region(new Complex(-2.5, -1), new Complex(1, 1), originAndWidth: false); stopWatch.Start(); var buffer = Mandelbrot.DrawMandelbrot( new Size(1, Environment.ProcessorCount), new Size(width, height), region, maxIterations, palette, gradient, 1E10); stopWatch.Stop(); CopyArrayToBitmap(width, height, depth, buffer, img); bmp.UnlockBits(img); var p = new PictureBox { Size = display._bmp.Size }; var form = new Form { Name = "Mandelbrot Display", Visible = false, AutoSize = true, Size = display._bmp.Size, KeyPreview = true }; form.KeyPress += display.Form_KeyPress; form.KeyDown += CloseOnEscapePressed; form.KeyDown += display.Form_KeyDown; void ResizeHandler(object sender, EventArgs e) { var size = form.Size; form.Visible = false; form.Close(); Main(new[] { size.Width.ToString(), size.Height.ToString() }); } void CloseOnEscapePressed(object sender, KeyEventArgs e) { if (e.KeyCode != Keys.Escape) { return; } form.Close(); e.Handled = true; } form.ResizeEnd += ResizeHandler; p.KeyPress += display.Form_KeyPress; p.KeyDown += display.Form_KeyDown; p.Image = display._bmp; form.Controls.Add(p); form.Icon = Icon.ExtractAssociatedIcon("Image.ico"); form.Text = $"Mandelbrot Set - Rendered in : {stopWatch.Elapsed}"; form.ShowDialog(); }