Another quick optimization for a hotspot in the GetPixel calls

This commit is contained in:
Michael Smith 2016-05-15 16:38:12 -07:00
parent fccd61f7f9
commit ff27687980

View File

@ -19,6 +19,7 @@ namespace OSCADSharp.Solids.Imported
private bool includeHeight; private bool includeHeight;
private Dictionary<Color, int> heightMappings; private Dictionary<Color, int> heightMappings;
List<OSCADObject> cubes = new List<OSCADObject>(); List<OSCADObject> cubes = new List<OSCADObject>();
private Color[,] pixels;
#endregion #endregion
#region Internal Fields #region Internal Fields
@ -42,6 +43,7 @@ namespace OSCADSharp.Solids.Imported
private List<OSCADObject> processImage() private List<OSCADObject> processImage()
{ {
Bitmap img = new Bitmap(Image.FromFile(this.imagePath)); Bitmap img = new Bitmap(Image.FromFile(this.imagePath));
this.setPixelArray(img);
this.setHeightMappings(img); this.setHeightMappings(img);
this.ImageBounds = new Bounds(new Vector3(), new Vector3(img.Width, img.Height, 1)); this.ImageBounds = new Bounds(new Vector3(), new Vector3(img.Width, img.Height, 1));
@ -51,7 +53,7 @@ namespace OSCADSharp.Solids.Imported
Point? start = this.getNextPoint(img, ref visited, img.Width - 1, img.Height - 1); Point? start = this.getNextPoint(img, ref visited, img.Width - 1, img.Height - 1);
do do
{ {
System.Drawing.Color color = img.GetPixel(((Point)start).X, ((Point)start).Y); System.Drawing.Color color = pixels[((Point)start).X, ((Point)start).Y];
var cube = this.traverseNext(img, (Point)start, ref visited, color); var cube = this.traverseNext(img, (Point)start, ref visited, color);
if (cube != null) if (cube != null)
@ -77,6 +79,18 @@ namespace OSCADSharp.Solids.Imported
return cubes; return cubes;
} }
private void setPixelArray(Bitmap img)
{
this.pixels = new Color[img.Width, img.Height];
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
pixels[x, y] = img.GetPixel(x, y);
}
}
}
private void setHeightMappings(Bitmap img) private void setHeightMappings(Bitmap img)
{ {
if (this.includeHeight) if (this.includeHeight)
@ -88,7 +102,7 @@ namespace OSCADSharp.Solids.Imported
{ {
for (int y = 0; y < img.Height; y++) for (int y = 0; y < img.Height; y++)
{ {
var color = img.GetPixel(x, y); var color = pixels[x, y];
double csum = (double)(color.R + color.G + color.B + color.A); double csum = (double)(color.R + color.G + color.B + color.A);
heightMappings[color] = Convert.ToInt32(csum != 0 ? (csum / max) * 10: .25); heightMappings[color] = Convert.ToInt32(csum != 0 ? (csum / max) * 10: .25);
} }
@ -177,7 +191,7 @@ namespace OSCADSharp.Solids.Imported
private bool pixelCanBeTraversed(Bitmap img, ref bool[,] visited, Point pixel, Color colorToMatch) private bool pixelCanBeTraversed(Bitmap img, ref bool[,] visited, Point pixel, Color colorToMatch)
{ {
return pixel.X < img.Width && pixel.Y < img.Height && return pixel.X < img.Width && pixel.Y < img.Height &&
visited[pixel.X, pixel.Y] == false && img.GetPixel(pixel.X, pixel.Y) == colorToMatch; visited[pixel.X, pixel.Y] == false && pixels[pixel.X, pixel.Y] == colorToMatch;
} }