Added a Grayscale import option

This commit is contained in:
Michael Smith 2016-05-15 22:09:13 -07:00
parent d308a517ee
commit c6347b0f93
3 changed files with 26 additions and 5 deletions

View File

@ -15,5 +15,10 @@ namespace OSCADSharp.Solids.Imported
/// Indicates whether height-mapping should be used /// Indicates whether height-mapping should be used
/// </summary> /// </summary>
public bool HeightMapping { get; set; } = true; public bool HeightMapping { get; set; } = true;
/// <summary>
/// Converts the colors in the image to black and white
/// </summary>
public bool UseGrayScale { get; set; } = false;
} }
} }

View File

@ -35,7 +35,7 @@ namespace OSCADSharp.Solids.Imported
options = new ImageImportOptions(); options = new ImageImportOptions();
} }
processor = new CubistImageProcessor(imagePath, options.HeightMapping); processor = new CubistImageProcessor(imagePath, options.HeightMapping, options.UseGrayScale);
var obj = processor.ProcessImage(); var obj = processor.ProcessImage();
var img = new ImportedImage() var img = new ImportedImage()

View File

@ -21,16 +21,18 @@ namespace OSCADSharp.Utility.Images
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; private Color[,] pixels;
private bool useGrayScale;
#endregion #endregion
#region Internal Fields #region Internal Fields
public Bounds ImageBounds { get; set; } public Bounds ImageBounds { get; set; }
#endregion #endregion
internal CubistImageProcessor(string imagePath, bool includeHeight = true) internal CubistImageProcessor(string imagePath, bool includeHeight = true, bool useGrayScale = false)
{ {
this.includeHeight = includeHeight; this.includeHeight = includeHeight;
this.imagePath = imagePath; this.imagePath = imagePath;
this.useGrayScale = useGrayScale;
} }
public OSCADObject ProcessImage() public OSCADObject ProcessImage()
@ -44,7 +46,7 @@ namespace OSCADSharp.Utility.Images
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.setColorArray(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));
@ -80,18 +82,32 @@ namespace OSCADSharp.Utility.Images
return cubes; return cubes;
} }
private void setPixelArray(Bitmap img) private void setColorArray(Bitmap img)
{ {
this.pixels = new Color[img.Width, img.Height]; this.pixels = new Color[img.Width, img.Height];
for (int x = 0; x < img.Width; x++) for (int x = 0; x < img.Width; x++)
{ {
for (int y = 0; y < img.Height; y++) for (int y = 0; y < img.Height; y++)
{ {
pixels[x, y] = img.GetPixel(x, y); setPixelColorValue(img, x, y);
} }
} }
} }
private void setPixelColorValue(Bitmap img, int x, int y)
{
if(this.useGrayScale)
{
Color rgbColor = img.GetPixel(x, y);
int grayscaleVal = (rgbColor.R + rgbColor.G + rgbColor.B) / 3;
pixels[x, y] = Color.FromArgb(rgbColor.A, grayscaleVal, grayscaleVal, grayscaleVal);
}
else
{
pixels[x, y] = img.GetPixel(x, y);
}
}
private void setHeightMappings(Bitmap img) private void setHeightMappings(Bitmap img)
{ {
if (this.includeHeight) if (this.includeHeight)