public override BitmapSource ExecuteFilter(BitmapSource inputImage) { byte[] sourceBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; byte[] destBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; inputImage.CopyPixels(sourceBytes, inputImage.PixelWidth * 4, 0); float redAdjust = 1.0f + (this.Warmth / 100.0f); float blueAdjust = 1.0f - (this.Warmth / 100.0f); GaussianBlurFilter blur = new GaussianBlurFilter(3 * Softness); BrightnessContrastFilter bc = new BrightnessContrastFilter(Lighting, -Lighting / 2); DesaturateFilter desat = new DesaturateFilter(); inputImage = blur.ExecuteFilter(inputImage); inputImage.CopyPixels(destBytes, inputImage.PixelWidth * 4, 0); int width = inputImage.PixelWidth; int height = inputImage.PixelHeight; for (int i = 0; i < sourceBytes.Length; i += 4) { byte b = sourceBytes[i]; byte g = sourceBytes[i + 1]; byte r = sourceBytes[i + 2]; byte a = sourceBytes[i + 3]; bc.PerformOperation(ref b, ref g, ref r, ref a); desat.PerformOperation(ref b, ref g, ref r, ref a); double rD = r * redAdjust; double bD = r * blueAdjust; if (rD > 255) { rD = 255; } else if (rD < 0) { rD = 0; } if (bD > 255) { bD = 255; } else if (bD < 0) { bD = 0; } destBytes[i] = PixelBlend.BlendOverlay(b, destBytes[i]); destBytes[i + 1] = PixelBlend.BlendOverlay(g, destBytes[i + 1]); destBytes[i + 2] = PixelBlend.BlendOverlay(r, destBytes[i + 2]); destBytes[i + 3] = PixelBlend.BlendOverlay(a, destBytes[i + 3]); } return(BitmapSource.Create(inputImage.PixelWidth, inputImage.PixelHeight, 96, 96, inputImage.Format, null, destBytes, inputImage.PixelWidth * 4)); }
public override BitmapSource ExecuteFilter(BitmapSource inputImage) { byte[] srcBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; byte[] finalBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; inputImage.CopyPixels(srcBytes, inputImage.PixelWidth * 4, 0); BrightnessContrastFilter bcFilter = new BrightnessContrastFilter(Brightness, Contrast); GaussianBlurFilter blurFilter = new GaussianBlurFilter(Radius); BitmapSource glow = blurFilter.ExecuteFilter(inputImage); byte[] glowBytes = new byte[glow.PixelWidth * glow.PixelHeight * 4]; glow.CopyPixels(glowBytes, glow.PixelWidth * 4, 0); for (int i = 0; i < glowBytes.Length; i += 4) { byte b = glowBytes[i]; byte g = glowBytes[i + 1]; byte r = glowBytes[i + 2]; byte a = glowBytes[i + 3]; bcFilter.PerformOperation(ref b, ref g, ref r, ref a); finalBytes[i] = PixelBlend.BlendScreen(srcBytes[i], b); finalBytes[i + 1] = PixelBlend.BlendScreen(srcBytes[i + 1], g); finalBytes[i + 2] = PixelBlend.BlendScreen(srcBytes[i + 2], r); finalBytes[i + 3] = 255; } return(BitmapSource.Create(inputImage.PixelWidth, inputImage.PixelHeight, 96, 96, inputImage.Format, null, finalBytes, inputImage.PixelWidth * 4)); }
public override BitmapSource ExecuteFilter(BitmapSource inputImage) { GlowEffect glowEffect = new GlowEffect(6, -(Coloring - 50) * 2, -(Coloring - 50) * 2); BitmapSource glow = glowEffect.ExecuteFilter(inputImage); byte[] glowBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; glow.CopyPixels(glowBytes, inputImage.PixelWidth * 4, 0); byte[] srcBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; inputImage.CopyPixels(srcBytes, inputImage.PixelWidth * 4, 0); byte[] finalBytes = new byte[inputImage.PixelWidth * inputImage.PixelHeight * 4]; int width = inputImage.PixelWidth; int height = inputImage.PixelHeight; // Create black outlines by finding the edges of objects for (int y = 0; y < height; y++) { int top = y - radius; int bottom = y + radius + 1; if (top < 0) { top = 0; } if (bottom > height) { bottom = height; } for (int x = 0; x < width; x++) { int left = x - radius; int right = x + radius + 1; if (left < 0) { left = 0; } if (right > width) { right = width; } int r = 0; int g = 0; int b = 0; for (int v = top; v < bottom; v++) { int j = v - y + radius; for (int u = left; u < right; u++) { int i1 = u - x + radius; int w = conv[j][i1]; int i = v * width * 4 + u * 4; r += srcBytes[i + 2] * w; g += srcBytes[i + 1] * w; b += srcBytes[i] * w; } } if (r > 255) { r = 255; } else if (r < 0) { r = 0; } if (g > 255) { g = 255; } else if (g < 0) { g = 0; } if (b > 255) { b = 255; } else if (b < 0) { b = 0; } byte rb = (byte)r; byte gb = (byte)g; byte bb = (byte)b; byte ab = (byte)255; // Desaturate DesaturateFilter desaturation = new DesaturateFilter(); desaturation.PerformOperation(ref bb, ref gb, ref rb, ref ab); // Adjust Brightness and Contrast if (rb > (InkOutline * 255 / 100)) { rb = gb = bb = 255; } else { rb = gb = bb = 0; } int index = y * width * 4 + x * 4; //Darken blend finalBytes[index] = PixelBlend.BlendDarken(glowBytes[index], bb); finalBytes[index + 1] = PixelBlend.BlendDarken(glowBytes[index + 1], gb); finalBytes[index + 2] = PixelBlend.BlendDarken(glowBytes[index + 2], rb); finalBytes[index + 3] = 255; } } return(BitmapSource.Create(inputImage.PixelWidth, inputImage.PixelHeight, 96, 96, inputImage.Format, null, finalBytes, inputImage.PixelWidth * 4)); }