Created
November 30, 2017 14:12
-
-
Save danwalmsley/9e0b013ed80cbad63fdade93c87e4d1a to your computer and use it in GitHub Desktop.
This file contains 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
var source = new Bitmap("sample.jpg"); | |
var pixelData = source.PlatformImpl.GetPixels(); | |
var stride = source.PixelWidth * 4; | |
float value = 1f; | |
IPixelFilter filter = new CompositeFilter(new ContrastFilter(10f), new GreenFilter()); | |
var start = DateTime.Now; | |
unsafe | |
{ | |
for (int y = 0; y < source.PixelHeight; ++y) | |
{ | |
byte* row = (byte*)pixelData + (y * stride); | |
int columnOffset = 0; | |
for (int x = 0; x < source.PixelWidth; ++x) | |
{ | |
var (r, g, b, a) = filter.Transform((row[columnOffset], row[columnOffset + 1], row[columnOffset + 2], row[columnOffset + 3])); | |
row[columnOffset] = r; | |
row[columnOffset + 1] = g; | |
row[columnOffset + 2] = b; | |
row[columnOffset + 3] = a; | |
columnOffset += 4; | |
} | |
} | |
} | |
var finish = DateTime.Now; | |
Console.WriteLine($"{(finish - start).Milliseconds}"); | |
ImageSource = source; | |
public interface IPixelFilter | |
{ | |
(byte r, byte g, byte b, byte a) Transform((byte r, byte g, byte b, byte a) pixel); | |
} | |
public class ContrastFilter : IPixelFilter | |
{ | |
public ContrastFilter (float contrastLevel) | |
{ | |
this.contrastLevel = contrastLevel; | |
} | |
private float contrastLevel; | |
public float ContrastLevel | |
{ | |
get { return contrastLevel; } | |
set { this.contrastLevel = value; } | |
} | |
public (byte r, byte g, byte b, byte a) Transform((byte r, byte g, byte b, byte a) pixel) | |
{ | |
float Red = pixel.r / 255.0f; | |
float Green = pixel.g / 255.0f; | |
float Blue = pixel.b / 255.0f; | |
Red = (((Red - 0.5f) * contrastLevel) + 0.5f) * 255.0f; | |
Green = (((Green - 0.5f) * contrastLevel) + 0.5f) * 255.0f; | |
Blue = (((Blue - 0.5f) * contrastLevel) + 0.5f) * 255.0f; | |
int iR = (int)Red; | |
iR = iR > 255 ? 255 : iR; | |
iR = iR < 0 ? 0 : iR; | |
int iG = (int)Green; | |
iG = iG > 255 ? 255 : iG; | |
iG = iG < 0 ? 0 : iG; | |
int iB = (int)Blue; | |
iB = iB > 255 ? 255 : iB; | |
iB = iB < 0 ? 0 : iB; | |
return ((byte)iR, (byte)iG, (byte)iB, pixel.a); | |
} | |
} | |
public class GreenFilter : IPixelFilter | |
{ | |
public (byte r, byte g, byte b, byte a) Transform((byte r, byte g, byte b, byte a) pixel) | |
{ | |
return (0, pixel.g, 0, pixel.a); | |
} | |
} | |
public class CompositeFilter : IPixelFilter | |
{ | |
private List<IPixelFilter> _filters; | |
public CompositeFilter(params IPixelFilter[] filters) | |
{ | |
_filters = new List<IPixelFilter>(filters); | |
} | |
public (byte r, byte g, byte b, byte a) Transform((byte r, byte g, byte b, byte a) pixel) | |
{ | |
foreach(var filter in _filters) | |
{ | |
pixel = filter.Transform(pixel); | |
} | |
return pixel; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment