public static BitmapSource GetBlueChannel(BitmapSource source) { var bytes = ToBytes(source); for (int blue = 0, green = 1, red = 2, alpha = 3; alpha < bytes.Length; blue += 4, green += 4, red += 4, alpha += 4) { bytes[green] = bytes[blue]; bytes[red] = bytes[blue]; } return(source.CloneWithAnotherBytes(bytes)); }
public static BitmapSource GetGrayscale(BitmapSource source) { var bytes = ToBytes(source); for (int blue = 0, green = 1, red = 2, alpha = 3; alpha < bytes.Length; blue += 4, green += 4, red += 4, alpha += 4) { var average = (byte)((bytes[blue] + bytes[green] + bytes[red]) / 3); bytes[blue] = average; bytes[red] = average; bytes[green] = average; } return(source.CloneWithAnotherBytes(bytes)); }
public static BitmapSource GetBitmapSourceWithReplacedRedChannel(BitmapSource source, BitmapSource additingSource) { var sourceBytes = ToBytes(source); byte[] additingBytes; if (Math.Abs(source.Width - additingSource.Width) > 1 || Math.Abs(source.Height - additingSource.Height) > 1) { additingBytes = ToBytes(CloneWithDifferentSize(additingSource, source.PixelWidth, source.PixelHeight)); } else { additingBytes = ToBytes(additingSource); } for (int blue = 0, green = 1, red = 2, alpha = 3; alpha < sourceBytes.Length && alpha < additingBytes.Length; blue += 4, green += 4, red += 4, alpha += 4) { var average = CalculateAverageByte(additingBytes[blue], additingBytes[green], additingBytes[red], additingBytes[alpha]); sourceBytes[red] = average; } return(source.CloneWithAnotherBytes(sourceBytes)); }