using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace ImageProcessingDemo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void OnFileOpen(object sender, EventArgs e)
        {
            if (openImgFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Image img = Image.FromFile(openImgFileDialog.FileName);
                    pictureBox.Image = img;
                    pictureBox.Width = img.Width;
                    pictureBox.Height = img.Height;
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message, "Ошибка загрузки изображения");
                }
            }
        }

		/// <summary>Выполняет фильтрацию изображения</summary>
		/// <param name="bmp">Исходное изображение</param>
		/// <param name="kernel">Ядро фильтра (3x3)</param>
		/// <returns>Обработанное изображение</returns>
		private Bitmap Filter(Bitmap bmp, double[,] kernel)
		{
			Bitmap result = new Bitmap(bmp.Width, bmp.Height);
			using (RandomAccessRGBBitmap src = new RandomAccessRGBBitmap(bmp))
			using (RandomAccessRGBBitmap dst = new RandomAccessRGBBitmap(result))
			{
				for (int x = 1; x < src.Width - 1; x++)
					for (int y = 1; y < src.Height - 1; y++)
					{
						double r = 0, g = 0, b = 0;
						for(int i = 0;i<3;i++)
							for(int j =0;j<3;j++) {
								Color c = src[x + i - 1,y + j - 1];
								r += c.R * kernel[i,j];
								g += c.G * kernel[i,j];
								b += c.B * kernel[i,j];
							}
						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;
						dst[x, y] = Color.FromArgb((int)r,(int)g,(int)b);
					}
			}
			return result;
		}

        private void OnFilterBlur(object sender, EventArgs e)
        {
            if (pictureBox.Image is Bitmap)
            {
				/*double[,] kernel = {
					{ 0,	1/6.0,	0 },
					{ 1/6.0,2/6.0,	1/6.0 },
					{ 0,	1/6.0,	0 }
				};*/

				double[,] kernel = {
					{ 0,	0,	0 },
					{ 0,	1,	0 },
					{ 0,	0,	0 }
				};
				pictureBox.Image = Filter((Bitmap)pictureBox.Image, kernel); 
            }
        }

        private Color FromArgb(int r, int g, int b)
        {
            return Color.FromArgb(Math.Min(255, Math.Max(0, r)), Math.Min(255, Math.Max(0, g)), Math.Min(255, Math.Max(0, b)));
        }

        private void OnFilterSharpen(object sender, EventArgs e)
        {
            if (pictureBox.Image is Bitmap)
            {
				double[,] kernel = {
					{ 0,	-1,	0 },
					{ -1,	5,	-1 },
					{ 0,	-1,	0 }
				};
				pictureBox.Image = Filter((Bitmap)pictureBox.Image, kernel);
			}
        }

        private void OnFilterEdges(object sender, EventArgs e)
        {
            if (pictureBox.Image is Bitmap)
            {
				double[,] kernel = {
					{ 0,	-1,	0 },
					{ -1,	4,	-1 },
					{ 0,	-1,	0 }
				};
				pictureBox.Image = Filter((Bitmap)pictureBox.Image, kernel); 

			}
        }
    } 
}