mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-12 14:08:35 +00:00
Added a Grayscale import option
This commit is contained in:
parent
d308a517ee
commit
c6347b0f93
@ -15,5 +15,10 @@ namespace OSCADSharp.Solids.Imported
|
||||
/// Indicates whether height-mapping should be used
|
||||
/// </summary>
|
||||
public bool HeightMapping { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Converts the colors in the image to black and white
|
||||
/// </summary>
|
||||
public bool UseGrayScale { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ namespace OSCADSharp.Solids.Imported
|
||||
options = new ImageImportOptions();
|
||||
}
|
||||
|
||||
processor = new CubistImageProcessor(imagePath, options.HeightMapping);
|
||||
processor = new CubistImageProcessor(imagePath, options.HeightMapping, options.UseGrayScale);
|
||||
var obj = processor.ProcessImage();
|
||||
|
||||
var img = new ImportedImage()
|
||||
|
||||
@ -21,16 +21,18 @@ namespace OSCADSharp.Utility.Images
|
||||
private Dictionary<Color, int> heightMappings;
|
||||
List<OSCADObject> cubes = new List<OSCADObject>();
|
||||
private Color[,] pixels;
|
||||
private bool useGrayScale;
|
||||
#endregion
|
||||
|
||||
#region Internal Fields
|
||||
public Bounds ImageBounds { get; set; }
|
||||
#endregion
|
||||
|
||||
internal CubistImageProcessor(string imagePath, bool includeHeight = true)
|
||||
internal CubistImageProcessor(string imagePath, bool includeHeight = true, bool useGrayScale = false)
|
||||
{
|
||||
this.includeHeight = includeHeight;
|
||||
this.imagePath = imagePath;
|
||||
this.useGrayScale = useGrayScale;
|
||||
}
|
||||
|
||||
public OSCADObject ProcessImage()
|
||||
@ -44,7 +46,7 @@ namespace OSCADSharp.Utility.Images
|
||||
private List<OSCADObject> processImage()
|
||||
{
|
||||
Bitmap img = new Bitmap(Image.FromFile(this.imagePath));
|
||||
this.setPixelArray(img);
|
||||
this.setColorArray(img);
|
||||
this.setHeightMappings(img);
|
||||
this.ImageBounds = new Bounds(new Vector3(), new Vector3(img.Width, img.Height, 1));
|
||||
|
||||
@ -80,18 +82,32 @@ namespace OSCADSharp.Utility.Images
|
||||
return cubes;
|
||||
}
|
||||
|
||||
private void setPixelArray(Bitmap img)
|
||||
private void setColorArray(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);
|
||||
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)
|
||||
{
|
||||
if (this.includeHeight)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user