public static FastBitmap ResultToBitmap(int[,] accumlator) { Bitmap r = new Bitmap(accumlator.GetLength(0), accumlator.GetLength(1)); FastBitmap resultBitmap = new FastBitmap(r); resultBitmap.Clear(Color.Black); using (resultBitmap.Lock()) { accumlator.Foreach((x, y, value) => { int accumulatedValue = accumlator[x, y]; int newGray = accumulatedValue < 0 ? 0 : (accumulatedValue > 255 ? 255 : accumulatedValue); resultBitmap.SetPixel(x, y, Color.FromArgb(255, newGray, newGray, newGray)); }); /* * for (int y = 0; y < wholeChunk.Height; y++) * { * for (int x = 0; x < wholeChunk.Width; x++) * { * int accumulatedValue = accumlator[x, y]; * int newGray = accumulatedValue < 0 ? 0 : (accumulatedValue > 255 ? 255 : accumulatedValue); * resultBitmap.SetPixel(x, y, Color.FromArgb(255, newGray, newGray, newGray)); * } * } */ } return(resultBitmap); }
private void RemoveLightButton_Click(object sender, EventArgs e) { int index = LightListBox.SelectedIndex; Lights.Remove(SelectedLight); if (index == Lights.Count) { if (Lights.Count == 0) { SelectedLight = null; RemoveLightButton.Enabled = false; LightGroupBox.Enabled = false; using (FastBitmap fast = ((Bitmap)LightColor.Image).FastLock()) { fast.Clear(Color.White); } } else { SelectedLight = Lights.Last(); } } else { SelectedLight = Lights[index == 0 ? index : index - 1]; } Lights.ResetBindings(); }
public void TestClearBitmap() { Bitmap bitmap = GenerateRandomBitmap(63, 63); // Non-dibisible by 8 bitmap, used to test loop unrolling FastBitmap.ClearBitmap(bitmap, Color.Red); // Loop through the image checking the pixels now for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { if (bitmap.GetPixel(x, y).ToArgb() != Color.Red.ToArgb()) { Assert.Fail( "Immediately after a call to FastBitmap.Clear(), all of the bitmap's pixels must be of the provided color"); } } } // Test an arbitratry color now FastBitmap.ClearBitmap(bitmap, Color.FromArgb(25, 12, 0, 42)); // Loop through the image checking the pixels now for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { if (bitmap.GetPixel(x, y).ToArgb() != Color.FromArgb(25, 12, 0, 42).ToArgb()) { Assert.Fail( "Immediately after a call to FastBitmap.Clear(), all of the bitmap's pixels must be of the provided color"); } } } // Test instance call FastBitmap fastBitmap = new FastBitmap(bitmap); fastBitmap.Clear(Color.FromArgb(25, 12, 0, 42)); Assert.IsFalse(fastBitmap.Locked, "After a successfull call to .Clear() on a fast bitmap previously unlocked, the .Locked property must be false"); // Loop through the image checking the pixels now for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { if (bitmap.GetPixel(x, y).ToArgb() != Color.FromArgb(25, 12, 0, 42).ToArgb()) { Assert.Fail( "Immediately after a call to FastBitmap.Clear(), all of the bitmap's pixels must be of the provided color"); } } } }
public void Clear_ShouldMakeAllPixelsZero() { FastBitmap bmp = FastBmp; bmp.Clear(); for (int i = 0; i < bmp.Length; i++) { Assert.Equal(0, bmp.Data[i]); } }
///------------------------------------------------------------------------------------------------- /// <summary> Loads the pixel data for the display window into the pixels[] array. </summary> /// /// <remarks> Jan Tamis, 27-8-2017. </remarks> public static void loadPixels() { // if (!buffer.IsLoaded) // { // buffer.Load(); // } // buffer.Clear(); //} img.Clear(Color.Transparent); img.Lock(); }
protected override void OnPaint(PaintEventArgs e) { if (e.ClipRectangle.IsEmpty) { return; } graphics.Graphics.ResetTransform(); surface.Clear(); baseControl.Render(graphics); surface.Flip(e.Graphics, e.ClipRectangle); }
private void LightColor_Click(object sender, EventArgs e) { ColorDialog dialog = new ColorDialog(); if (dialog.ShowDialog() == DialogResult.OK) { SelectedLight.Color = dialog.Color; using (FastBitmap fast = ((Bitmap)LightColor.Image).FastLock()) { fast.Clear(dialog.Color); } LightColor.Refresh(); } }
public static void Clear(Bitmap bitmap) { using (FastBitmap fast = bitmap.FastLock()) { fast.Clear(Color.FromArgb(25, 25, 50)); } Depth = new float[bitmap.Width][]; Depth[0] = new float[bitmap.Height]; for (int i = 0; i < bitmap.Height; i++) { Depth[0][i] = float.MaxValue; } for (int i = 1; i < bitmap.Width; i++) { Depth[i] = new float[bitmap.Height]; Array.Copy(Depth[0], 0, Depth[i], 0, bitmap.Height); } }
public static Bitmap Create(World world) { var canvas = new Bitmap(world.Width, world.Height); var bitmap = new FastBitmap(canvas); bitmap.Lock(); bitmap.Clear(world.BackgroundColor); foreach (var block in world.Blocks.Where(block => colors[block.Type] != 0u).OrderByDescending(x => x.Layer >= 1)) { foreach (var location in block.Locations) { bitmap.SetPixel(location.X, location.Y, colors[block.Type]); } } bitmap.Unlock(); return(canvas); }
private void LightListBox_SelectedIndexChanged(object sender, EventArgs e) { if (Lights.Count == 0 || LightListBox.SelectedIndex == -1) { return; } SelectedLight = Lights[LightListBox.SelectedIndex]; using (FastBitmap fast = ((Bitmap)LightColor.Image).FastLock()) { fast.Clear(SelectedLight.Color); } LightIntensity.Value = (decimal)SelectedLight.Intensity; LightRange.Value = (decimal)SelectedLight.Range; LightPosX.Value = (decimal)SelectedLight.Position.X; LightPosY.Value = (decimal)SelectedLight.Position.Y; LightPosZ.Value = (decimal)SelectedLight.Position.Z; LightColor.Refresh(); }
private void ColorImage(short[] colorMap) { using (FastBitmap fimg = _colorImage.FastLock()) { fimg.Clear(Color.Transparent); for (int x = 0; x < _territoryBitmapSize.Width; x++) { for (int y = 0; y < _territoryBitmapSize.Height; y++) { short rank = _mapData[x, y]; if (rank >= 0 && rank < colorMap.Length && colorMap[rank] >= 0 && colorMap[rank] < _samples.Length) { Color c = _samples[colorMap[rank]].GetPixel(x, y); fimg.SetPixel(x, y, c); } } } } spTerritories.BaseImage = _colorImage; }
void ParseJSON() { #region description /* How the JSON is set up: * * [...] * x : VALUE * y : VALUE * z : VLAUE * [...] * * We need x and z. * The StreamReader reads it like this: * x * VALUE * y * VALUE * z * VALUE * .. So we look for "x" only (skiptoX = true), if we find it we use the next reading iteration for its value (thats why there is the boolean "lastX"), skip 3 iterations (for y ; VALUE; z;) and take the next value. * */ #endregion bool skiptoX = true, lastX = false; byte count = 0; long x = 0, y = 0; JsonTextReader streamReader = new JsonTextReader(new StreamReader(settings.Path_json)); while (streamReader.Read()) { if (streamReader.Value != null) { if (skiptoX) { if (String.Equals(streamReader.Value, "x")) { lastX = true; skiptoX = false; } } else if (lastX) { x = Convert.ToInt64(streamReader.Value); lastX = false; count = 1; } else if (count > 0 && count < 4) // Count 3 Iterations (we starts with count = 1) { count++; } else if (count == 4) // After 3 Iterations, we read the value and pass it and x to AddToBitmap method while displaying the process on the Console. { y = Convert.ToInt64(streamReader.Value); skiptoX = true; count = 0; int _x = ((int)x / settings.Ly_to_px + settings.Img_X_offset); int _y = ((int)y / settings.Ly_to_px + settings.Img_Y_offset); if (_x > 0 && _y > 0 && _x < settings.Img_Xres && _y < settings.Img_Yres) { sysmap[_x - 1][(_y - 1) + 2 * (settings.Img_Yres / 2 - _y)]++; } counter++; if (counter % 10045 == 0) { Dispatcher.BeginInvoke((Action)(() => { Write($"{counter} Systems parsed."); })); } } } } Dispatcher.BeginInvoke((Action)(() => { Write($"{counter} Systems parsed. \n Applying to Bitmap."); })); // Plot 2dArray onto bitmap, using selected method bitmap = new FastBitmap((int)settings.Img_Xres, (int)settings.Img_Yres); /* * 1:Greyscale * 2:Hue * 3:Hue+Value * 4:r * 5:g * 6:b * 7:a */ #region using Greyscale if (settings.Rendering == 1 || settings.Rendering == 0) { int index = 0; bitmap.Clear(FastColor.black); for (int iteration_x = 0; iteration_x < sysmap.Length; iteration_x++) { for (int iteration_y = 0; iteration_y < sysmap[iteration_x].Length; iteration_y++) { uint value = Convert.ToUInt32(sysmap[iteration_x][iteration_y] * settings.Systems_per_ColorVal); byte greyscale; if (value > 255) { greyscale = 255; } else { greyscale = (byte)value; } bitmap.SetPixel(iteration_x + 1, iteration_y + 1, new FastColor(greyscale, greyscale, greyscale)); } } index++; if (index % 10045 == 0) { Dispatcher.BeginInvoke((Action)(() => { Write($"{index} pixels set."); })); } } #endregion #region using Hue(but no Value) else if (settings.Rendering == 2) { int index = 0; bitmap.Clear(FastColor.black); for (int iteration_x = 0; iteration_x < sysmap.Length; iteration_x++) { for (int iteration_y = 0; iteration_y < sysmap[iteration_x].Length; iteration_y++) { uint value = Convert.ToUInt32(sysmap[iteration_x][iteration_y] * settings.Systems_per_ColorVal); if (value > 180) { value = value - 180; } // MOve Hue by 180 Degreen so blue is 0 and Red is maximum else { value = value + 180; } var color = new Hsv((value + 180) / 2, 1, 1).ToRgb(); bitmap.SetPixel(iteration_x + 1, iteration_y + 1, new FastColor((byte)color.R, (byte)color.G, (byte)color.B)); } } index++; if (index % 10045 == 0) { Dispatcher.BeginInvoke((Action)(() => { Write($"{index} pixels set."); })); } } #endregion #region using Hue and Value else if (settings.Rendering == 3) { int index = 0; bitmap.Clear(FastColor.black); for (int iteration_x = 0; iteration_x < sysmap.Length; iteration_x++) { for (int iteration_y = 0; iteration_y < sysmap[iteration_x].Length; iteration_y++) { uint value = Convert.ToUInt32(sysmap[iteration_x][iteration_y] * settings.Systems_per_ColorVal); uint value_greyscale; if (value > 360) { value_greyscale = 255; } else if (value > 255) { value_greyscale = 225; } else { value_greyscale = value; } var color = new Hsv((value + 180) / 2, 1, value_greyscale).ToRgb(); bitmap.SetPixel(iteration_x + 1, iteration_y + 1, new FastColor((byte)color.R, (byte)color.G, (byte)color.B)); } } index++; if (index % 10045 == 0) { Dispatcher.BeginInvoke((Action)(() => { Write($"{index} pixels set."); })); } } #endregion #region using Red else if (settings.Rendering == 4) { int index = 0; bitmap.Clear(FastColor.black); for (int iteration_x = 0; iteration_x < sysmap.Length; iteration_x++) { for (int iteration_y = 0; iteration_y < sysmap[iteration_x].Length; iteration_y++) { uint value = Convert.ToUInt32(sysmap[iteration_x][iteration_y] * settings.Systems_per_ColorVal); byte greyscale; if (value > 255) { greyscale = 255; } else { greyscale = (byte)value; } bitmap.SetPixel(iteration_x + 1, iteration_y + 1, new FastColor(greyscale, 0, 0)); } } index++; if (index % 10045 == 0) { Dispatcher.BeginInvoke((Action)(() => { Write($"{index} pixels set."); })); } } #endregion #region using Green else if (settings.Rendering == 5) { int index = 0; bitmap.Clear(FastColor.black); for (int iteration_x = 0; iteration_x < sysmap.Length; iteration_x++) { for (int iteration_y = 0; iteration_y < sysmap[iteration_x].Length; iteration_y++) { uint value = Convert.ToUInt32(sysmap[iteration_x][iteration_y] * settings.Systems_per_ColorVal); byte greyscale; if (value > 255) { greyscale = 255; } else { greyscale = (byte)value; } bitmap.SetPixel(iteration_x + 1, iteration_y + 1, new FastColor(0, greyscale, 0)); } } index++; if (index % 10045 == 0) { Dispatcher.BeginInvoke((Action)(() => { Write($"{index} pixels set."); })); } } #endregion #region using Blue else if (settings.Rendering == 6) { int index = 0; bitmap.Clear(FastColor.black); for (int iteration_x = 0; iteration_x < sysmap.Length; iteration_x++) { for (int iteration_y = 0; iteration_y < sysmap[iteration_x].Length; iteration_y++) { uint value = Convert.ToUInt32(sysmap[iteration_x][iteration_y] * settings.Systems_per_ColorVal); byte greyscale; if (value > 255) { greyscale = 255; } else { greyscale = (byte)value; } bitmap.SetPixel(iteration_x + 1, iteration_y + 1, new FastColor(0, 0, greyscale)); } } index++; if (index % 10045 == 0) { Dispatcher.BeginInvoke((Action)(() => { Write($"{index} pixels set."); })); } } #endregion string file = settings.Path_output + "/" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".png"; bitmap.Save(file); bitmap = null; sysmap = null; Dispatcher.BeginInvoke((Action)(() => { Write($"Image was sucessfully generated."); loadingicon.Visibility = Visibility.Hidden; })); System.Diagnostics.Process.Start(file); // Display image with default Image Displaying Software }
private void FigureDataGrid_SelectionChanged(object sender, EventArgs e) { CuboidGroupBox.Visible = false; SphereGroupBox.Visible = false; CylinderGroupBox.Visible = false; ConeGroupBox.Visible = false; if (FigureDataGrid.SelectedRows.Count != 0) { SelectedFigure = (IFigure)FigureDataGrid.SelectedRows[0].DataBoundItem; if (SelectedFigure is Cuboid) { Cuboid cube = SelectedFigure as Cuboid; CuboidGroupBox.Visible = true; CuboidPosX.Value = (decimal)cube.Center.X; CuboidPosY.Value = (decimal)cube.Center.Y; CuboidPosZ.Value = (decimal)cube.Center.Z; CuboidDimX.Value = (decimal)cube.DimX; CuboidDimY.Value = (decimal)cube.DimY; CuboidDimZ.Value = (decimal)cube.DimZ; CuboidRotX.Value = (decimal)(cube.RotX * 180f / Math.PI); CuboidRotY.Value = (decimal)(cube.RotY * 180f / Math.PI); CuboidRotZ.Value = (decimal)(cube.RotZ * 180f / Math.PI); CuboidX.Value = (decimal)cube.X; CuboidY.Value = (decimal)cube.Y; CuboidZ.Value = (decimal)cube.Z; using (FastBitmap fast = ((Bitmap)CuboidColor.Image).FastLock()) { fast.Clear(cube.Color); } CuboidColor.Refresh(); } else if (SelectedFigure is Sphere) { SphereGroupBox.Visible = true; Sphere sphere = SelectedFigure as Sphere; SpherePosX.Value = (decimal)sphere.Center.X; SpherePosY.Value = (decimal)sphere.Center.Y; SpherePosZ.Value = (decimal)sphere.Center.Z; SphereDimX.Value = (decimal)sphere.DimX; SphereDimY.Value = (decimal)sphere.DimY; SphereDimZ.Value = (decimal)sphere.DimZ; SphereRotX.Value = (decimal)(sphere.RotX * 180f / Math.PI); SphereRotY.Value = (decimal)(sphere.RotY * 180f / Math.PI); SphereRotZ.Value = (decimal)(sphere.RotZ * 180f / Math.PI); SphereLat.Value = (decimal)sphere.Lat; SphereLon.Value = (decimal)sphere.Lon; SphereRadius.Value = (decimal)sphere.Radius; using (FastBitmap fast = ((Bitmap)SphereColor.Image).FastLock()) { fast.Clear(sphere.Color); } SphereColor.Refresh(); } else if (SelectedFigure is Cylinder) { CylinderGroupBox.Visible = true; Cylinder cylinder = SelectedFigure as Cylinder; CylinderPosX.Value = (decimal)cylinder.Center.X; CylinderPosY.Value = (decimal)cylinder.Center.Y; CylinderPosZ.Value = (decimal)cylinder.Center.Z; CylinderDimX.Value = (decimal)cylinder.DimX; CylinderDimY.Value = (decimal)cylinder.DimY; CylinderDimZ.Value = (decimal)cylinder.DimZ; CylinderRotX.Value = (decimal)(cylinder.RotX * 180f / Math.PI); CylinderRotY.Value = (decimal)(cylinder.RotY * 180f / Math.PI); CylinderRotZ.Value = (decimal)(cylinder.RotZ * 180f / Math.PI); CylinderDiv.Value = (decimal)cylinder.Division; CylinderHeight.Value = (decimal)cylinder.Height; CylinderRadius.Value = (decimal)cylinder.Radius; using (FastBitmap fast = ((Bitmap)CylinderColor.Image).FastLock()) { fast.Clear(cylinder.Color); } CylinderColor.Refresh(); } else if (SelectedFigure is Cone) { ConeGroupBox.Visible = true; Cone cone = SelectedFigure as Cone; ConePosX.Value = (decimal)cone.Center.X; ConePosY.Value = (decimal)cone.Center.Y; ConePosZ.Value = (decimal)cone.Center.Z; ConeDimX.Value = (decimal)cone.DimX; ConeDimY.Value = (decimal)cone.DimY; ConeDimZ.Value = (decimal)cone.DimZ; ConeRotX.Value = (decimal)(cone.RotX * 180f / Math.PI); ConeRotY.Value = (decimal)(cone.RotY * 180f / Math.PI); ConeRotZ.Value = (decimal)(cone.RotZ * 180f / Math.PI); ConeDiv.Value = (decimal)cone.Division; ConeHeight.Value = (decimal)cone.Height; ConeRadius.Value = (decimal)cone.Radius; using (FastBitmap fast = ((Bitmap)ConeColor.Image).FastLock()) { fast.Clear(cone.Color); } ConeColor.Refresh(); } } }