Created
August 13, 2016 04:05
-
-
Save CVertex/e17d4831d94a6b93f87b57f807237baf to your computer and use it in GitHub Desktop.
My first imageprocessor IGraphicsProcessor
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
| public class OverlayWithLinearGradient : IGraphicsProcessor | |
| { | |
| public OverlayWithLinearGradient() | |
| { | |
| this.DynamicParameter = Tuple.Create(Color.PaleVioletRed, Color.CornflowerBlue); | |
| this.Settings = new Dictionary<string, string>(); | |
| } | |
| public dynamic DynamicParameter | |
| { | |
| get; | |
| set; | |
| } | |
| public Dictionary<string, string> Settings | |
| { | |
| get; | |
| set; | |
| } | |
| public Image ProcessImage(ImageFactory factory) | |
| { | |
| // Greyscale image | |
| var greyscale = factory.Saturation(-100); // desaturate for now | |
| var source = greyscale.Image; | |
| // Create gradient, same size as image | |
| var gradientColors = (Tuple<Color, Color>)this.DynamicParameter; | |
| var gradient = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppPArgb); | |
| gradient.SetResolution(source.HorizontalResolution, source.VerticalResolution); | |
| gradient = Effects.LinearGradient(gradient, gradientColors.Item1, gradientColors.Item2); | |
| // Multiply greyscale x gradient | |
| try | |
| { | |
| using (FastBitmap sourceBitmap = new FastBitmap(source)) | |
| { | |
| for (var x = 0; x<sourceBitmap.Width; x++) | |
| { | |
| for (var y = 0; y < sourceBitmap.Height; y++) | |
| { | |
| var srcColor = sourceBitmap.GetPixel(x, y); | |
| var destColor = gradient.GetPixel(x, y); | |
| // Instead of div by 255, work in 16bit, then 2 shifts and 2 adds to 8bit | |
| int ta = (srcColor.A * destColor.A) + 128; | |
| int tr = (srcColor.R * destColor.R) + 128; | |
| int tg = (srcColor.G * destColor.G) + 128; | |
| int tb = (srcColor.B * destColor.B) + 128; | |
| byte ba = (byte)(((ta >> 8) + ta) >> 8); | |
| byte br = (byte)(((tr >> 8) + tr) >> 8); | |
| byte bg = (byte)(((tg >> 8) + tg) >> 8); | |
| byte bb = (byte)(((tb >> 8) + tb) >> 8); | |
| // Update pixel | |
| sourceBitmap.SetPixel(x, y, Color.FromArgb(ba, br,bg,bb)); | |
| } | |
| } | |
| } | |
| return source; | |
| } | |
| catch (Exception ex) | |
| { | |
| throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have an example of the expected output (maybe using CSS blend mode to compare against?)