48215 (608498), страница 3
Текст из файла (страница 3)
using System.Windows.Forms;
namespace Kursach
{
public partial class MainForm : Form
{
private string filename = "";
WaitForm win = null;
public MainForm()
{
InitializeComponent();
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
picture.Image = new Bitmap(filename = openFileDialog.FileName);
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
picture.Image.Save(filename = saveFileDialog.FileName);
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
picture.Image.Save(filename);
}
private void лапласианToolStripMenuItem_Click(object sender, EventArgs e)
{
Start(false);
}
public void Start(bool edit) where T : IKernel, new()
{
IKernel kernel = new T();
Bitmap bitmap = new Bitmap(picture.Image);
if (new KernelForm(false, kernel).ShowDialog() == DialogResult.OK)
{
win = new WaitForm(backgroundWorker);
win.Show(this);
backgroundWorker.RunWorkerAsync(new StartParam(bitmap, kernel));
}
}
private void smoothingToolStripMenuItem_Click(object sender, EventArgs e)
{
Start(false);
}
private void гаусовыйToolStripMenuItem_Click(object sender, EventArgs e)
{
Start(false);
}
private void CustomToolStripMenuItem_Click(object sender, EventArgs e)
{
Start(true);
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
StartParam s = (StartParam)e.Argument;
pictureF.Image = Filter.Filtering(s.b, s.k.Kernel , backgroundWorker);
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
win.progressBar.Value = e.ProgressPercentage;
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
win.Close();
}
}
class StartParam
{
public Bitmap b = null;
public IKernel k = null;
public StartParam(Bitmap b, IKernel k)
{
this.b = b;
this.k = k;
}
}
}
WaitForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Kursach
{
public partial class WaitForm : Form
{
BackgroundWorker bw;
public WaitForm(BackgroundWorker bw)
{
InitializeComponent();
this.bw = bw;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void WaitForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (bw.IsBusy)
bw.CancelAsync();
}
}
}
KernelForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Kursach
{
public partial class KernelForm : Form
{
private bool allowEdit = false;
public bool AllowEdit
{
get { return allowEdit; }
set
{
allowEdit = value;
kernelgrid.ReadOnly = !value;
}
}
public KernelForm(bool editable, IKernel kernel)
{
InitializeComponent();
for (int i = 0; i < kernel.Kernel.GetUpperBound(0) + 1; i++)
{
kernelgrid.Rows.Add();
for (int j = 0; j < kernel.Kernel.GetUpperBound(1) + 1; j++)
{
kernelgrid.Rows[i].Cells[j].Value = kernel.Kernel.GetValue(i, j);
}
}
this.Text = kernel.Text;
AllowEdit = editable;
}
private void butCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void butOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
this.Close();
}
}
}
Filter.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
namespace Kursach
{
static class Filter
{
public static Bitmap Filtering(Bitmap img, int[,] kernel, BackgroundWorker bw)
{
int[,] Mk = (int[,])kernel.Clone();
int[] H = new int[kernel.Length];
int counter = 0;
for (int i = 0; i <= kernel.GetUpperBound(0); i++)
for (int j = 0; j <= kernel.GetUpperBound(1); j++)
H[counter++] = kernel[i, j];
Bitmap ret = new Bitmap(img);
int norma = Max(Sum(H), 1);
for (int i = 2; i < img.Width - 2; i++)
{
for (int j = 2; j < img.Height - 2; j++)
{
int[] V = new int[25];
for (int k = 0; k < V.Length; k++)
V[k] = img.GetPixel(i - 1 + k / 5 - 1, j - 1 + k % 5 - 1).ToArgb();
long mul = MulScalar(V, H);
double d =(double)1 / (double)norma;
double px = (d * mul);
ret.SetPixel(i, j, Color.FromArgb((int)px));
}
bw.ReportProgress(100 * i / img.Width);
if (bw.CancellationPending)
return ret;
}
return ret;
}
private static int Sum(int[] arr)
{
int res = 0;
foreach (int i in arr)
res += i;
return res;
}
private static int Max(int first, int second)
{
if (first > second)
return first;
else
return second;
}
private static long MulScalar(int[] first, int[] second)
{
long res = 0;
for (int i = 0; i < first.Length; i++)
res += (first[i] * second[i]);
return res;
}
}
}
Kernel.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Kursach
{
public interface IKernel
{
int[,] Kernel
{
get;
}
string Text
{
get;
}
}
}
Kernels.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Kursach
{
class Gradient : IKernel
{
#region IKernel Members
public int[,] Kernel
{
get
{
return new int[,]
{ { 0, -1, 0, 1, 0},
{-1, -2, 0, 2, 1},
{-1, -2, 0, 2, 1},
{-1, -2, 0, 2, 1},
{0, -1, 0, 1, 0} };
}
}
public string Text
{
get { return "Градиентный"; }
}
#endregion
}
class Laplacian : IKernel
{
#region IKernel Members
public int[,] Kernel
{
get
{
return new int[,]
{ { -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1},
{-1, -1, 24, -1, -1},
{-1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1}}; ;
}
}
public string Text
{
get { return "Лапласиана"; }
}
#endregion
}
class Smoothing : IKernel
{
#region IKernel Members
public int[,] Kernel
{
get
{
return new int[,]
{ { 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 0, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1}};
}
}
public string Text
{
get { return "Smoothing"; }
}
#endregion
}
class Gaussian : IKernel
{
#region IKernel Members
public int[,] Kernel
{
get
{
return new int[,]
{{ 1, 2, 4, 2, 1},
{2, 4, 8, 4, 2},
{4, 8, 16, 8, 16},
{2, 4, 8, 4, 2},
{1, 2, 4, 2, 1}};
}
}
public string Text
{
get { return "Гаусовый"; }
}
#endregion
}
class Unknown : IKernel
{
#region IKernel Members
public int[,] Kernel
{
get
{
return new int[,]
{ { 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};
}
}
public string Text
{
get { return "Произвольный"; }
}
#endregion
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Kursach
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}












