unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, ExtCtrls, Math, TypesUnit, ParamUnit,
  DirectionsUnit, LinearUnit, ImageUnit, HistoUnit, WeightImageUnit,
  CompareUnit;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    Save1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    Filter1: TMenuItem;
    Adaptive1: TMenuItem;
    Showweights1: TMenuItem;
    Linear1: TMenuItem;
    Sequence1: TMenuItem;
    Setparameters1: TMenuItem;
    Histogram1: TMenuItem;
    Histogram2: TMenuItem;
    Window1: TMenuItem;
    Cascade1: TMenuItem;
    Tile1: TMenuItem;
    CloseAll1: TMenuItem;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Label1: TLabel;
    Label2: TLabel;
    procedure Open1Click(Sender: TObject);
    procedure Save1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure Adaptive1Click(Sender: TObject);
    procedure Showweights1Click(Sender: TObject);
    procedure Linear1Click(Sender: TObject);
    procedure Sequence1Click(Sender: TObject);
    procedure Setparameters1Click(Sender: TObject);
    procedure Histogram2Click(Sender: TObject);
    procedure Cascade1Click(Sender: TObject);
    procedure Tile1Click(Sender: TObject);
    procedure CloseAll1Click(Sender: TObject);
    procedure MainFormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure SetParameters(Param: Word);
  public
    procedure Thresholding(Threshold: Integer);
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.Open1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    if FileExists(OpenDialog1.FileName) then
      with TImageForm.Create(Self, OpenDialog1.FileName) do
        Caption:=ExtractFileName(OpenDialog1.FileName);
end;


procedure TMainForm.Save1Click(Sender: TObject);
var InIF: TImageForm;
begin
  if MDIChildCount = 0 then Exit;
  InIF:=ActiveMDIChild as TImageForm;

  if SaveDialog1.Execute then
    InIF.Image.Picture.Bitmap.SaveToFile(SaveDialog1.FileName);
end;


procedure TMainForm.Exit1Click(Sender: TObject);
begin
  Close;
end;


procedure TMainForm.Adaptive1Click(Sender: TObject);
var InIF: TImageForm;
begin
  if MDIChildCount = 0 then Exit;
  InIF:=ActiveMDIChild as TImageForm;

  if Directions = nil then
    SetParameters(1);
  if Directions = nil then
    Exit;

  Screen.Cursor:=crHourGlass;
  with TImageForm.Create(Self, Directions.Filtering(InIF.ImageBr)) do
    Caption:=InIF.Caption + ' - ad. filtrováno';
  Screen.Cursor:=crDefault;
end;


procedure TMainForm.Showweights1Click(Sender: TObject);
begin
  if Directions = nil then
    SetParameters(1);
  if Directions = nil then
    Exit;

  if WeightImageForm = nil then
    WeightImageForm:=TWeightImageForm.Create(Self)
  else
    WeightImageForm.Close;
end;


procedure TMainForm.Linear1Click(Sender: TObject);
var InIF: TImageForm;
begin
  if MDIChildCount = 0 then Exit;
  InIF:=ActiveMDIChild as TImageForm;

  if Linear = nil then
    SetParameters(2);
  if Linear = nil then
    Exit;

  Screen.Cursor:=crHourGlass;
  with TImageForm.Create(Self, Linear.Filtering(InIF.ImageBr)) do
    Caption:=InIF.Caption + ' - lin. filtrováno';
  Screen.Cursor:=crDefault;
end;


procedure TMainForm.Sequence1Click(Sender: TObject);
begin
  if MDIChildCount = 0 then Exit;

  TCompareForm.Create(Self, ActiveMDIChild as TImageForm);
end;


procedure TMainForm.Setparameters1Click(Sender: TObject);
begin
  SetParameters(0);
end;


procedure TMainForm.Histogram2Click(Sender: TObject);
begin
  if MDIChildCount = 0 then Exit;
  (ActiveMDIChild as TImageForm).MakeHistogram(True);
end;


procedure TMainForm.Cascade1Click(Sender: TObject);
begin
  Cascade;
end;


procedure TMainForm.Tile1Click(Sender: TObject);
begin
  Tile;
end;


procedure TMainForm.CloseAll1Click(Sender: TObject);
var i: Integer;
begin
  for i:=0 to MDIChildCount-1 do
    MDIChildren[i].Close;
end;


procedure TMainForm.MainFormClose(Sender: TObject; var Action: TCloseAction);
begin
  try
    if Directions <> nil then
      Directions.Destroy;
    if Linear <> nil then
      Linear.Destroy;
  finally
    Action:=caFree;
  end;
end;


procedure TMainForm.SetParameters(Param: Word);
begin
  with TParamForm.Create(Self, Param) do begin
    if ShowModal = mrOk then begin
      if AdaptiveRButton.Checked then begin
        if Directions <> nil then
          Directions.Destroy;
        Directions:=TDirections.Create(StrToInt(Edit1.Text),StrToFloat(Edit2.Text),StrToFloat(Edit3.Text));
        MainForm.Label1.Caption:='Adaptivní filtr:     počet směrů: '+Edit1.Text+';   vzdálenost: '+Edit2.Text+';   sigma: '+Edit3.Text;
      end;
      if LinearRButton.Checked then begin
        if Linear <> nil then
          Linear.Destroy;
        Linear:=TLinear.Create(StrToFloat(Edit2.Text),StrToFloat(Edit3.Text));
        MainForm.Label2.Caption:='Lineární filtr:     vzdálenost: '+Edit2.Text+';   sigma: '+Edit3.Text;
      end;
    end;
    Release;
    Close;
  end;
end;


procedure TMainForm.Thresholding(Threshold: Integer);
var ImF: TImageForm;
begin
  if Screen.ActiveForm.ClassType <> THistoForm then Exit;
  ImF:=Screen.ActiveForm.Owner as TImageForm;

  with TImageForm.Create(Self, ImF.Thresholding(Threshold)) do
    Caption:=ImF.Caption + ' - prahováno';
end;

end.
