/// <summary> /// Sets the pixel color at the specified coordinates. /// </summary> /// <param name="x">The X coordinate</param> /// <param name="y">The Y coordinate</param> /// <param name="c">The pixel color</param> public void SetPixel(int x, int y, FastColor c) { x = Mathf.Clamp(x, 0, Width - 1); y = Mathf.Clamp(y, 0, Width - 1); data[x, y] = c; }
/// <summary> /// Initializes a new instance of FastBitmap with a specified clear color. /// </summary> /// <param name="width">The width in pixels of the FastBitmap</param> /// <param name="height">The height in pixels of the FastBitmap</param> /// <param name="color">The clear color</param> public FastBitmap(int width, int height, FastColor color) { Width = width; Height = height; data = new FastColor[width, height]; Clear(color); Effects = new ImageEffects(this); }
// METHODS & FUNCTIONS /// <summary> /// Clears the image with a specified color. /// </summary> /// <param name="color">The color</param> public void Clear(FastColor color) { for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { data[x, y] = color; } } }
/// <summary> /// Inverts the image. /// </summary> public void Invert() { for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { FastColor color = f.GetPixel(x, y); FastColor invertedColor = new FastColor((byte)(255 - color.R), (byte)(255 - color.G), (byte)(255 - color.B)); f.SetPixel(x, y, invertedColor); } } }
/// <summary> /// Sets the luminosity of the image. /// </summary> /// <param name="luminosityFactor">The luminosity factor</param> public void Luminosity(float luminosityFactor) { for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { FastColor c = f.GetPixel(x, y); c = new FastColor( Mathf.ClampByte((byte)(c.R * luminosityFactor)), Mathf.ClampByte((byte)(c.G * luminosityFactor)), Mathf.ClampByte((byte)(c.B * luminosityFactor))); f.SetPixel(x, y, c); } } }
/// <summary> /// Detects edges in your image, uses a 3x3 kerning. (Atrociously slow too, I really need to find better ways at this) /// </summary> public void EdgeDetection() { FastColor[,] finalData = new FastColor[f.Width, f.Height]; for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { FastColor[] temp = new FastColor[3 * 3]; int tempIndex = 0; for (int x2 = -1; x2 <= 1; x2++) // 3x3 kerning { for (int y2 = -1; y2 <= 1; y2++) { temp[tempIndex++] = f.GetPixel(x + x2, y + y2); } } byte[] rArr = temp.Select(o => o.R).ToArray(); byte[] gArr = temp.Select(o => o.G).ToArray(); byte[] bArr = temp.Select(o => o.B).ToArray(); byte rMin = Mathf.Min(rArr); byte gMin = Mathf.Min(gArr); byte bMin = Mathf.Min(bArr); byte rMax = Mathf.Max(rArr); byte gMax = Mathf.Max(gArr); byte bMax = Mathf.Max(bArr); byte final = Mathf.Max((byte)(rMax - rMin), (byte)(gMax - gMin), (byte)(bMax - bMin)); finalData[x, y] = new FastColor(final, final, final); } } for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { f.SetPixel(x, y, finalData[x, y]); } } }
/// <summary> /// Blurs the image using the Box Blur technique, currently atrociously slow. /// </summary> /// <param name="blurSize">The size of the blur, a higher size means more blurring.</param> public void BoxBlur(int blurSize) // todo: Make this effect sonic fast. { if (blurSize < 1) { return; } FastColor[,] finalData = new FastColor[f.Width, f.Height]; for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { float r = 0, g = 0, b = 0; int blurDivision = 0; for (int x2 = -blurSize; x2 <= blurSize; x2++) { for (int y2 = -blurSize; y2 <= blurSize; y2++) { Color c = f.GetPixel(x + x2, y + y2); r += c.R; g += c.G; b += c.B; blurDivision++; } } r /= blurDivision; g /= blurDivision; b /= blurDivision; finalData[x, y] = new FastColor((byte)r, (byte)g, (byte)b); } } // Final pass, feed blurred data to FastBitmap for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { f.SetPixel(x, y, finalData[x, y]); } } }
static void Main(string[] args) { FastColor c = new FastColor(127, 192, 255); Console.WriteLine("Please select an image."); OpenFileDialog ofd = new OpenFileDialog() { Filter = "BMP Files|*.bmp|JPG Files|*.jpg|PNG Files|*.png", CheckFileExists = true, CheckPathExists = true }; SaveFileDialog sfd = new SaveFileDialog() { Filter = "BMP Files|*.bmp", CheckPathExists = true }; if (ofd.ShowDialog() == DialogResult.OK) { FastBitmap f = FastBitmap.FromFile(ofd.FileName); // Edge detection, yay! f.Effects.EdgeDetection(); Console.WriteLine("Please select the save path"); if (sfd.ShowDialog() == DialogResult.OK) { f.Save(sfd.FileName); } } }
/// <summary> /// Blurs the image using the Box Blur technique, currently atrociously slow. /// </summary> /// <param name="blurSize">The size of the blur, a higher size means more blurring.</param> public void BoxBlur(int blurSize) // todo: Make this effect sonic fast. { if (blurSize < 1) return; FastColor[,] finalData = new FastColor[f.Width, f.Height]; for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { float r = 0, g = 0, b = 0; int blurDivision = 0; for (int x2 = -blurSize; x2 <= blurSize; x2++) { for (int y2 = -blurSize; y2 <= blurSize; y2++) { Color c = f.GetPixel(x + x2, y + y2); r += c.R; g += c.G; b += c.B; blurDivision++; } } r /= blurDivision; g /= blurDivision; b /= blurDivision; finalData[x, y] = new FastColor((byte)r, (byte)g, (byte)b); } } // Final pass, feed blurred data to FastBitmap for (int x = 0; x < f.Width; x++) { for (int y = 0; y < f.Height; y++) { f.SetPixel(x, y, finalData[x, y]); } } }