Last active
May 5, 2019 12:43
-
-
Save antonfirsov/ba3b3fc05e7aba19458d6d61c8b38e2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Contains a general pixel-format independant color definition | |
public struct Color | |
{ | |
private Rgba64 data; // Could be also Vector4, but that would probably lead to compatibility issues. | |
public Color(Rgba64 pixel) { this.data = pixel; } | |
public Color(Rgba32 pixel) { this.data.FromRgba32(pixel); } | |
public Color(Vector4 vector) { this.data.FromVector4(vector); } | |
public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(new Rgba32(r, g, b, a)); | |
public static Color FromRgba(short r, short g, short b, short a) => new Color(new Rgba64(r, g, b, a)); | |
// + All kinds of other constructors and factory methods .... | |
// Static instances | |
public static readonly Color Black = FromRgba(...); | |
public static readonly Color Gray = FromRgba(...); | |
public static readonly Color White = FromRgba(...); | |
public Rgba64 ToPixel<TPixel>() | |
{ | |
TPixel pixel = default; | |
pixel.FromRgba64(this.data); | |
return pixel; | |
} | |
} | |
// Definition of the processor: | |
public class BinaryThresholdProcessor : IImageProcessor | |
{ | |
public float Threshold { get; } | |
public Color LowerColor { get; } | |
public Color UpperColor { get; } | |
public CreatePixelSpecificProcessor<TPixel>() => new BinaryThresholdProcessor<TPixel>(this); | |
} | |
public BinaryThresholdProcessor<TPixel> : ImageProcessor<TPixel> | |
{ | |
private float threshold; | |
private TPixel lowerColor; | |
private TPixel upperColor; | |
public BinaryThresholdProcessor(BinaryThresholdProcessor definition) | |
{ | |
this.threshold = definition.Threshold; | |
this.lowerColor = definition.LowerColor.ToPixel<TPixel>(); | |
this.upperColor = definition.UpperColor.ToPixel<TPixel>(); | |
} | |
} | |
// Usage of the processor: | |
using (Image image = Image.Load("foo.jpg")) | |
{ | |
image.BinaryThreshold(0.5F, Color.White, Color.Gray); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment