unit CompareUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Math, TypesUnit, DirectionsUnit, LinearUnit, ImageUnit,
  Gauges, ExtCtrls;

type
  TCompareImageForm = class(TImageForm)
      procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
      pDir: Integer;
      pDis: Double;
      pSig: Double;
    protected
      procedure VisibleChanging; override;
    public
      constructor Create(AOwner: TComponent; ImBr: TByteMatrix; Dr: Integer; Ds, Sg: Double); reintroduce;
      property Dir: Integer read pDir;
      property Dis: Double read pDis;
      property Sig: Double read pSig;
    end;

  TCompareForm = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    ListBox1: TListBox;
    Gauge1: TGauge;
    RadioGroup1: TRadioGroup;
    RadioGroup2: TRadioGroup;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure KeyPressed(Sender: TObject; var Key: Char);
    procedure KeyPressedDeci(Sender: TObject; var Key: Char);
    procedure RadioGroup2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    ImBr: TByteMatrix;
    Cap: String;
    Dir0, Dir1: Integer;
    Dis0, Dis1: Double;
    Sig0, Sig1: Double;
    DirN, DisN, SigN: Integer;
    Stop: Boolean;
    procedure MakeAll;
  public
    constructor Create(AOwner: TComponent; InIF: TImageForm); reintroduce;
  end;

implementation

uses HistoUnit;

{$R *.dfm}

constructor TCompareImageForm.Create(AOwner: TComponent; ImBr: TByteMatrix; Dr: Integer; Ds, Sg: Double);
begin
  inherited Create(AOwner, ImBr);
  pDir:=Dr;
  pDis:=Ds;
  pSig:=Sg;
end;


procedure TCompareImageForm.VisibleChanging;
begin
  //tato funkce prázdná umožní zneviditelňovat formy TImageForm stylu MDIchild,
  //původně zde bylo: Raise ...
end;


procedure TCompareImageForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  HistoForm.Visible:=False;
  Action:=caHide;
end;


constructor TCompareForm.Create(AOwner: TComponent; InIF: TImageForm);
begin
  inherited Create(AOwner);
  ImBr:=InIF.ImageBr;
  Cap:=InIF.Caption;
  Caption:=InIF.Caption+' - srovnávání';
  Stop:=False;
end;


procedure TCompareForm.MakeAll;
var i, j, k, l, Dir: Integer;
    Dis, Sig: Double;
    ImF: TCompareImageForm;
    Direct: TDirections;
    Lin: TLinear;
label GoToLabel;
begin
  Gauge1.MaxValue:=Round(DirN*DisN*SigN);
  Gauge1.Progress:=0;
  Button1.Enabled:=False;
  Button3.Enabled:=True;

  Dir:=Dir0;
  for i:=0 to DirN-1 do begin
    Dis:=Dis0;
    for j:=0 to DisN-1 do begin
      Sig:=Sig0;
      for k:=0 to SigN-1 do begin

        for l:=0 to ListBox1.Items.Count-1 do begin
          ImF:=ListBox1.Items.Objects[l] as TCompareImageForm;
          if (Dir = ImF.Dir) and (Dis = ImF.Dis) and (Sig = ImF.Sig) then
            GoTo GoToLabel;
        end;

        case RadioGroup2.ItemIndex of
          0 : begin
                Direct:=TDirections.Create(Dir, Dis, Sig);
                ImF:=TCompareImageForm.Create(Self, Direct.Filtering(ImBr), Dir, Dis, Sig);
                with ImF do begin
                  Visible:=False;
                  Caption:='N: '+IntToStr(ImF.Dir)+';   D: '+FloatToStr(ImF.Dis)+';   Sigma: '+FloatToStr(ImF.Sig);
                  MakeHistogram(False);
                  Caption:=Cap+' - bimodalita: '+IntToStr(ImF.Bimodality)+';   směrů: '+IntToStr(ImF.Dir)+';   vzdálenost: '+FloatToStr(ImF.Dis)+';   sigma: '+FloatToStr(ImF.Sig);
                end;
                ListBox1.Items.AddObject(ImF.Caption, ImF);
                Direct.Destroy;
              end;

          1 : begin
                Lin:=TLinear.Create(Dis, Sig);
                ImF:=TCompareImageForm.Create(Self, Lin.Filtering(ImBr), Dir, Dis, Sig);
                with ImF do begin
                  Visible:=False;
                  Caption:='D: '+FloatToStr(ImF.Dis)+';   Sigma: '+FloatToStr(ImF.Sig);
                  MakeHistogram(False);
                  Caption:=Cap+' - bimodalita: '+IntToStr(ImF.Bimodality)+';   vzdálenost: '+FloatToStr(ImF.Dis)+';   sigma: '+FloatToStr(ImF.Sig);
                end;
                ListBox1.Items.AddObject(ImF.Caption, ImF);
                Lin.Destroy;
              end;
        end;

        GoToLabel:
        Gauge1.Progress:=Gauge1.Progress+1;
        Application.ProcessMessages;
        if Stop then begin
          Button1.Enabled:=True;
          Button3.Enabled:=False;
          Stop:=False;
          Gauge1.Progress:=0;
          Exit;
        end;

        Sig:=Sig+Sig1;
      end;
      Dis:=Dis+Dis1;
    end;
    Dir:=Dir+Dir1;
  end;

  Button1.Enabled:=True;
  Button3.Enabled:=False;
end;


procedure TCompareForm.KeyPressed(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', #8]) then
    Key:=#0;
end;


procedure TCompareForm.KeyPressedDeci(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', #8, ',']) or ((Key = ',') and (Pos(Key, (Sender as TEdit).Text) <> 0)) then
    Key:=#0;
end;


procedure TCompareForm.RadioGroup2Click(Sender: TObject);
begin
  case RadioGroup2.ItemIndex of
    0 : begin
          Edit1.Enabled:=True;
          Edit2.Enabled:=True;
          Edit3.Enabled:=True;
        end;
    1 : begin
          Edit1.Enabled:=False;
          Edit2.Enabled:=False;
          Edit3.Enabled:=False;
        end;
  end;
end;


procedure TCompareForm.Button1Click(Sender: TObject);
var Dir2: Integer;
    Dis2, Sig2: Double;
begin
  if RadioGroup2.ItemIndex = 0 then begin
    Dir0:=StrToInt(Edit1.Text);
    Dir1:=StrToInt(Edit2.Text);
    Dir2:=StrToInt(Edit3.Text);
    if Dir0 = 0 then begin
      ShowMessage('Počet směrů nesmí být 0.');
      Edit1.SetFocus;
      Exit;
    end;
    if Dir2 > 128 then begin
      ShowMessage('Počet směrů smí být maximálně 128.');
      Edit3.SetFocus;
      Exit;
    end;
    if Dir2 < Dir0 then begin
      ShowMessage('Začátek a/nebo konec počtu směrů je zadán špatně - konec musí být větší nebo roven než začátek.');
      Edit1.SetFocus;
      Exit;
    end;
    end
  else begin
    Dir0:=0;
    Dir1:=0;
    Dir2:=0;
  end;

  Dis0:=StrToFloat(Edit4.Text);
  Dis1:=StrToFloat(Edit5.Text);
  Dis2:=StrToFloat(Edit6.Text);
  if Dis0 <= 0.5 then begin
    ShowMessage('Vzdálenost 0,5 nebo menší v obrazu nezpůsobí změny.');
    Edit4.SetFocus;
    Exit;
  end;
  if Dis2 > 128 then begin
    ShowMessage('Vzdálenost smí být maximálně 128.');
    Edit6.SetFocus;
    Exit;
  end;
  if Dis2 < Dis0 then begin
    ShowMessage('Začátek a/nebo konec vzdálenosti je zadán špatně - konec musí být větší nebo roven než začátek.');
    Edit4.SetFocus;
    Exit;
  end;

  Sig0:=StrToFloat(Edit7.Text);
  Sig1:=StrToFloat(Edit8.Text);
  Sig2:=StrToFloat(Edit9.Text);
  if Sig0 = 0 then begin
    ShowMessage('Sigma nesmí být 0.');
    Edit7.SetFocus;
    Exit;
  end;
  if Sig2 < Sig0 then begin
    ShowMessage('Začátek a konec sigma je zadán špatně - konec musí být větší nebo roven než začátek.');
    Edit7.SetFocus;
    Exit;
  end;

  if Dir1 = 0 then
    DirN:=1
  else
    DirN:=RoundUp((Dir2-Dir0)/Dir1)+1;

  if Dis1 = 0 then
    DisN:=1
  else
    DisN:=RoundUp((Dis2-Dis0)/Dis1)+1;

  if Sig1 = 0 then
    SigN:=1
  else
    SigN:=RoundUp((Sig2-Sig0)/Sig1)+1;

  MakeAll;
end;


procedure TCompareForm.Button2Click(Sender: TObject);
var i, n: Integer;
    ImPointers: array of TCompareImageForm;
begin
  n:=ListBox1.Items.Count;
  SetLength(ImPointers, n);

  for i:=0 to n-1 do
    ImPointers[i]:=ListBox1.Items.Objects[i] as TCompareImageForm;

  ListBox1.Clear;

  ListBox1.Sorted:=True;
  for i:=0 to n-1 do
    case RadioGroup1.ItemIndex of
      0 : ListBox1.Items.AddObject(SysUtils.Format('%.15d',[ImPointers[i].Bimodality]), ImPointers[i]);
      1 : ListBox1.Items.AddObject(SysUtils.FormatFloat('000000000000000.000000', ImPointers[i].Dis), ImPointers[i]);
      2 : ListBox1.Items.AddObject(SysUtils.Format('%.15d',[ImPointers[i].Dir]), ImPointers[i]);
      3 : ListBox1.Items.AddObject(SysUtils.FormatFloat('000000000000000.000000', ImPointers[i].Sig), ImPointers[i]);
    end;
  ListBox1.Sorted:=False;

  for i:=0 to n-1 do
    ListBox1.Items.Strings[i]:=(ListBox1.Items.Objects[i] as TCompareImageForm).Caption;
end;


procedure TCompareForm.Button3Click(Sender: TObject);
begin
  Stop:=True;
end;


procedure TCompareForm.ListBox1Click(Sender: TObject);
var ImF: TCompareImageForm;
begin
  ImF:=ListBox1.Items.Objects[ListBox1.ItemIndex] as TCompareImageForm;
  ImF.Visible:=True;
  ImF.MakeHistogram(True);
end;


procedure TCompareForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

end.
