/// <summary> /// change the contrast of an image /// </summary> /// <param name="Img">image to manipilate</param> /// <param name="contrast">value of contrast</param> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to image /// Filter.Contrast(ref Img, 50.0); /// ]]> /// </code> /// </example> /// <see cref="contrast"/> /// <returns></returns> static public void Contrast(ref ImageData Img, double contrast) { double RedChannel, GreenChannel, BlueChannel; Color PixelColor; contrast = (100.0 + contrast) / 100.0; contrast *= contrast; for (int y = 0; y < Img.Height; y++) { for (int x = 0; x < Img.Width; x++) { PixelColor = Img[x, y]; RedChannel = (((PixelColor.R / 255.0) - 0.5) * contrast + 0.5) * 255; GreenChannel = (((PixelColor.G / 255.0) - 0.5) * contrast + 0.5) * 255; BlueChannel = (((PixelColor.B / 255.0) - 0.5) * contrast + 0.5) * 255; RedChannel = (RedChannel > 255) ? 255 : RedChannel; RedChannel = (RedChannel < 0) ? 0 : RedChannel; GreenChannel = (GreenChannel > 255) ? 255 : GreenChannel; GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel; BlueChannel = (BlueChannel > 255) ? 255 : BlueChannel; BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel; Img.SetPixel(x, y, Color.FromArgb((int)RedChannel, (int)GreenChannel, (int)BlueChannel)); } } }
/// <summary> /// change the brightness of an image /// </summary> /// <param name="Img">image to manipulate</param> /// <param name="brightness">brightness of new image</param> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to image /// Filter.Brightness(ref Img, 10); /// ]]> /// </code> /// </example> /// <see cref="brightness"/> /// <returns></returns> static public void Brightness(ref ImageData Img, int brightness) { int RedChannel, GreenChannel, BlueChannel; Color PixelColor; for (int y = 0; y < Img.Height; y++) { for (int x = 0; x < Img.Width; x++) { PixelColor = Img[x, y]; RedChannel = PixelColor.R + brightness; GreenChannel = PixelColor.G + brightness; BlueChannel = PixelColor.B + brightness; RedChannel = (RedChannel > 255) ? 255 : RedChannel; RedChannel = (RedChannel < 0) ? 0 : RedChannel; GreenChannel = (GreenChannel > 255) ? 255 : GreenChannel; GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel; BlueChannel = (BlueChannel > 255) ? 255 : BlueChannel; BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel; Img.SetPixel(x, y, Color.FromArgb(RedChannel, GreenChannel, BlueChannel)); } } }
/// <summary> /// Convert image to sepia /// </summary> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to original and convert to sepia /// Filter.Sepia(ref Img); /// ]]> /// </code> /// </example> /// <see cref="sepia"/> /// <param name="Img">ref image to convert</param> /// <returns></returns> static public void Sepia(ref ImageData Img) { int t; for (Int32 column = 0; column < Img.Width; column++) { for (Int32 row = 0; row < Img.Height; row++) { Color c = Img[column, row]; t = Convert.ToInt32(0.299 * c.R + 0.587 * c.G + 0.114 * c.B); Img.SetPixel(column, row, Color.FromArgb(((t > 206) ? 255 : t + 49), ((t < 14) ? 0 : t - 14), ((t < 56) ? 0 : t - 56))); } } }
/// <summary> /// decrease the depth of color /// </summary> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to image /// Filter.DecreaseColourDepth(ref Img, 40); /// ]]> /// </code> /// </example> /// <param name="Img">image to manipulate</param> /// <param name="offset">offset of colordepth</param> /// <see cref="decreasecolourdepth"/> /// <returns></returns> static public void DecreaseColourDepth(ref ImageData Img, int offset) { int RedChannel, GreenChannel, BlueChannel; Color PixelColor; for (int y = 0; y < Img.Height; y++) { for (int x = 0; x < Img.Width; x++) { PixelColor = Img[x, y]; RedChannel = ((PixelColor.R + (offset / 2)) - ((PixelColor.R + (offset / 2)) % offset) - 1); GreenChannel = ((PixelColor.G + (offset / 2)) - ((PixelColor.G + (offset / 2)) % offset) - 1); BlueChannel = ((PixelColor.B + (offset / 2)) - ((PixelColor.B + (offset / 2)) % offset) - 1); RedChannel = (RedChannel < 0) ? 0 : RedChannel; GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel; BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel; Img.SetPixel(x, y, Color.FromArgb(RedChannel, GreenChannel, BlueChannel)); } } }
static private void ApplyConvolution3x3(ref ImageData Img, ConvolutionMatrix m) { ImageData newImg = Img.Clone(); Color[,] pixelColor = new Color[3, 3]; int A, RedChannel, GreenChannel, BlueChannel; for (int y = 0; y < Img.Height - 2; y++) { for (int x = 0; x < Img.Width - 2; x++) { pixelColor[0, 0] = Img[x, y]; pixelColor[0, 1] = Img[x, y + 1]; pixelColor[0, 2] = Img[x, y + 2]; pixelColor[1, 0] = Img[x + 1, y]; pixelColor[1, 1] = Img[x + 1, y + 1]; pixelColor[1, 2] = Img[x + 1, y + 2]; pixelColor[2, 0] = Img[x + 2, y]; pixelColor[2, 1] = Img[x + 2, y + 1]; pixelColor[2, 2] = Img[x + 2, y + 2]; A = pixelColor[1, 1].A; RedChannel = (int)((((pixelColor[0, 0].R * m.Matrix[0, 0]) + (pixelColor[1, 0].R * m.Matrix[1, 0]) + (pixelColor[2, 0].R * m.Matrix[2, 0]) + (pixelColor[0, 1].R * m.Matrix[0, 1]) + (pixelColor[1, 1].R * m.Matrix[1, 1]) + (pixelColor[2, 1].R * m.Matrix[2, 1]) + (pixelColor[0, 2].R * m.Matrix[0, 2]) + (pixelColor[1, 2].R * m.Matrix[1, 2]) + (pixelColor[2, 2].R * m.Matrix[2, 2])) / m.Factor) + m.Offset); GreenChannel = (int)((((pixelColor[0, 0].G * m.Matrix[0, 0]) + (pixelColor[1, 0].G * m.Matrix[1, 0]) + (pixelColor[2, 0].G * m.Matrix[2, 0]) + (pixelColor[0, 1].G * m.Matrix[0, 1]) + (pixelColor[1, 1].G * m.Matrix[1, 1]) + (pixelColor[2, 1].G * m.Matrix[2, 1]) + (pixelColor[0, 2].G * m.Matrix[0, 2]) + (pixelColor[1, 2].G * m.Matrix[1, 2]) + (pixelColor[2, 2].G * m.Matrix[2, 2])) / m.Factor) + m.Offset); BlueChannel = (int)((((pixelColor[0, 0].B * m.Matrix[0, 0]) + (pixelColor[1, 0].B * m.Matrix[1, 0]) + (pixelColor[2, 0].B * m.Matrix[2, 0]) + (pixelColor[0, 1].B * m.Matrix[0, 1]) + (pixelColor[1, 1].B * m.Matrix[1, 1]) + (pixelColor[2, 1].B * m.Matrix[2, 1]) + (pixelColor[0, 2].B * m.Matrix[0, 2]) + (pixelColor[1, 2].B * m.Matrix[1, 2]) + (pixelColor[2, 2].B * m.Matrix[2, 2])) / m.Factor) + m.Offset); RedChannel = (RedChannel > 255) ? 255 : RedChannel; RedChannel = (RedChannel < 0) ? 0 : RedChannel; GreenChannel = (GreenChannel > 255) ? 255 : GreenChannel; GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel; BlueChannel = (BlueChannel > 255) ? 255 : BlueChannel; BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel; newImg.SetPixel(x + 1, y + 1, Color.FromArgb(A, RedChannel, GreenChannel, BlueChannel)); } } Img = newImg.Clone(); }
/// <summary> /// Convert image to sepia /// </summary> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to original and convert to sepia /// Filter.Sepia(ref Img); /// ]]> /// </code> /// </example> /// <see cref="sepia"/> /// <param name="Img">ref image to convert</param> /// <returns></returns> public static void Sepia(ref ImageData Img) { int t; for (Int32 column = 0; column < Img.Width; column++) { for (Int32 row = 0; row < Img.Height; row++) { Color c = Img[column, row]; t = Convert.ToInt32(0.299 * c.R + 0.587 * c.G + 0.114 * c.B); Img.SetPixel(column, row, Color.FromArgb(((t > 206) ? 255 : t + 49), ((t < 14) ? 0 : t - 14), ((t < 56) ? 0 : t - 56))); } } }
/// <summary> /// decrease the depth of color /// </summary> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to image /// Filter.DecreaseColourDepth(ref Img, 40); /// ]]> /// </code> /// </example> /// <param name="Img">image to manipulate</param> /// <param name="offset">offset of colordepth</param> /// <see cref="decreasecolourdepth"/> /// <returns></returns> public static void DecreaseColourDepth(ref ImageData Img, int offset) { int RedChannel, GreenChannel, BlueChannel; Color PixelColor; for (int y = 0; y < Img.Height; y++) { for (int x = 0; x < Img.Width; x++) { PixelColor = Img[x, y]; RedChannel = ((PixelColor.R + (offset / 2)) - ((PixelColor.R + (offset / 2)) % offset) - 1); GreenChannel = ((PixelColor.G + (offset / 2)) - ((PixelColor.G + (offset / 2)) % offset) - 1); BlueChannel = ((PixelColor.B + (offset / 2)) - ((PixelColor.B + (offset / 2)) % offset) - 1); RedChannel = (RedChannel < 0) ? 0 : RedChannel; GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel; BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel; Img.SetPixel(x, y, Color.FromArgb( RedChannel, GreenChannel, BlueChannel)); } } }
/// <summary> /// change the contrast of an image /// </summary> /// <param name="Img">image to manipilate</param> /// <param name="contrast">value of contrast</param> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to image /// Filter.Contrast(ref Img, 50.0); /// ]]> /// </code> /// </example> /// <see cref="contrast"/> /// <returns></returns> public static void Contrast(ref ImageData Img, double contrast) { double RedChannel, GreenChannel, BlueChannel; Color PixelColor; contrast = (100.0 + contrast) / 100.0; contrast *= contrast; for (int y = 0; y < Img.Height; y++) { for (int x = 0; x < Img.Width; x++) { PixelColor = Img[x, y]; RedChannel = (((PixelColor.R / 255.0) - 0.5) * contrast + 0.5) * 255; GreenChannel = (((PixelColor.G / 255.0) - 0.5) * contrast + 0.5) * 255; BlueChannel = (((PixelColor.B / 255.0) - 0.5) * contrast + 0.5) * 255; RedChannel = (RedChannel > 255) ? 255 : RedChannel; RedChannel = (RedChannel < 0) ? 0 : RedChannel; GreenChannel = (GreenChannel > 255) ? 255 : GreenChannel; GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel; BlueChannel = (BlueChannel > 255) ? 255 : BlueChannel; BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel; Img.SetPixel(x, y, Color.FromArgb((int)RedChannel, (int)GreenChannel, (int)BlueChannel)); } } }
/// <summary> /// change the brightness of an image /// </summary> /// <param name="Img">image to manipulate</param> /// <param name="brightness">brightness of new image</param> /// <example> /// <code> /// <![CDATA[ /// ImageData Img = new ImageData(...); /// // apply filter to image /// Filter.Brightness(ref Img, 10); /// ]]> /// </code> /// </example> /// <see cref="brightness"/> /// <returns></returns> public static void Brightness(ref ImageData Img, int brightness) { int RedChannel, GreenChannel, BlueChannel; Color PixelColor; for (int y = 0; y < Img.Height; y++) { for (int x = 0; x < Img.Width; x++) { PixelColor = Img[x, y]; RedChannel = PixelColor.R + brightness; GreenChannel = PixelColor.G + brightness; BlueChannel = PixelColor.B + brightness; RedChannel = (RedChannel > 255) ? 255 : RedChannel; RedChannel = (RedChannel < 0) ? 0 : RedChannel; GreenChannel = (GreenChannel > 255) ? 255 : GreenChannel; GreenChannel = (GreenChannel < 0) ? 0 : GreenChannel; BlueChannel = (BlueChannel > 255) ? 255 : BlueChannel; BlueChannel = (BlueChannel < 0) ? 0 : BlueChannel; Img.SetPixel(x, y, Color.FromArgb( RedChannel, GreenChannel, BlueChannel)); } } }