unit MainUnit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, openTIFF, ExtCtrls,ImageUnit, Menus, MaticeUnit, GraphUnit,
  ComCtrls, ExtDlgs, ToolWin, ModelUnit;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    Soubor1: TMenuItem;
    ListBox1: TListBox;
    OpenDialog1: TOpenDialog;
    Panel2: TPanel;
    Image1: TImage;
    TrackBar1: TTrackBar;
    TrackBar2: TTrackBar;
    Label2: TLabel;
    ScrollBox1: TScrollBox;
    Shape1: TShape;
    Analzaobrazu1: TMenuItem;
    Otevt1: TMenuItem;
    Label1: TLabel;
    RadioGroup1: TRadioGroup;
    RadioGroup2: TRadioGroup;
    Exporthodnot1: TMenuItem;
    SaveTextFileDialog1: TSaveTextFileDialog;
    Graf1: TMenuItem;
    N2: TMenuItem;
    Otevt2: TMenuItem;
    Vytvoitvkovprofil1: TMenuItem;
    Konvoluce1: TMenuItem;
    Exportjakostl1: TMenuItem;
    N3Dmodel1: TMenuItem;
    Uloitaktulnsnmek1: TMenuItem;
    SavePictureDialog1: TSavePictureDialog;
    procedure Otevt1Click(Sender: TObject); //Načtení obrázků do pole Bitmap
    procedure ListBox1DblClick(Sender: TObject); //Vybrat vzorovy obrazek
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer); //Hue v kliku a z intervalu
    procedure TrackBar1Change(Sender: TObject); //Zmena intervalu vyberu hue
    procedure RadioGroup1Click(Sender: TObject); //Nastavení tvaru okolí
    procedure RadioGroup2Click(Sender: TObject); //Nastavení velikosti okolí
    procedure Analzaobrazu1Click(Sender: TObject); //Analýza obrazu
    function ZobrazitTif(Image16Array: PImage16ArrayRGB; Tiffinf: TTIFFinfo): TBitmap;
    procedure ZobrazPixely(Image16Array: PImage16ArrayRGB; Tiffinf: TTIFFinfo; H: Double);
    procedure Konec1Click(Sender: TObject);
    procedure Exporthodnot1Click(Sender: TObject);
    procedure Otevt2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Vytvoitvkovprofil1Click(Sender: TObject);
    procedure N3Dmodel1Click(Sender: TObject);
    procedure Uloitaktulnsnmek1Click(Sender: TObject);
    procedure Konvoluce1Click(Sender: TObject);

  private
    { Private declarations }
    CisloVzor: Integer;
    bAnalyza,
    bKlik,
    bVysProf,
    bZobrVysProf,
    bGraf: Boolean;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  Indexes: TIndexes;
  obrazek: Array of PImage16ArrayRGB;
  snimek: PImage16ArrayRGB;
  VyskaVeSnimku: TMatice;
  //VyskaVeSnimku: Array of Array of Single;
  ZnamVysku: Array of Array of Boolean;
  TIFFinfo: Array of TTIFFinfo;
  Vzorovy, bmpVysProfil: TBitmap;
  nacteni:bool;
  MinY,MaxY,MinX,MaxX:integer;
  apar, bpar, cpar, dpar, epar: Double;
  PrumerHue: TMatice;
  function MaximumRGB(R, G, B: Word): Word;
  function MinimumRGB(R, G, B: Word): Word;
  function Hue(R, G, B: Word): Double;
  function Jas(R, G, B: Word): Double;
  function InvFunkce(a, b, c, d, e, y: Double): Double;
implementation

uses Math;

{$R *.DFM}

//procedure TMainForm.Button3Click(Sender: TObject); <- ulozeni barevneho tiffu
//begin
//  ImageToTIFF16(Image16Array,Image1.Picture.Bitmap.Width,Image1.Picture.Bitmap.Height,False,'ulozeny.tiff');
//end;

//procedure TMainForm.Button4Click(Sender: TObject); <-ulozeni sedeho tiffu
//begin
//  ImageToTIFF16(Image16Array,Image1.Picture.Bitmap.Width,Image1.Picture.Bitmap.Height,True,'ulozeny_gray.tiff');
//end;


//procedure TMainForm.Button5Click(Sender: TObject);
//var Adr,i,j: Integer;
//    SL: PByteArray;
//begin
//  TestTIFFile('pokus8RGB.tif',TIFFinfo);
//  TIFFCount  v teto promenne je pocet TIFFu v MultiTIFFu

// W:=GetTIFFWidth(TIFFinfo: TTIFFinfo; Index: Word): Integer;    //sirka snimku
// H:=GetTIFFHeight(TIFFinfo: TTIFFinfo; Index: Word): Integer;   //vyska snimku

//  LoadFromFileTIF('pokus8RGB.tif',1,Image16Array,TIFFinfo);
//  Image1.Picture.Create;
//  Image1.Picture.Bitmap.Create;
//  Image1.Picture.Bitmap.PixelFormat:=pf24bit;
//  Image1.Picture.Bitmap.Width:=TIFFinfo[1].Width.Value;
//  Image1.Picture.Bitmap.Height:=TIFFinfo[1].Height.Value;
//  if TypeTIFF=imRGB then
//    begin
//      for j:=0 to TIFFinfo[1].Height.Value-1 do
//        begin
//          SL:=Image1.Picture.Bitmap.Scanline[j];
//          Adr:=j*TIFFinfo[1].Width.Value*3;
//          for i:=0 to TIFFinfo[1].Width.Value*3-1 do
//            begin
//              SL^[i]:=Round(Image16Array^[Adr+i])
//            end;
//        end;
//    end;
//  Image1.Repaint;
//end;

{procedure TMainForm.Otevt1Click(Sender: TObject);
var i,j:integer;
    neni:bool;
begin
  if OpenDialog1.Execute then begin
  for i:= 0 to OpenDialog1.Files.Count -1 do begin
    if FileExists(OpenDialog1.Files[i]) then begin
      neni := true;
      for j:=0 to ListBox1.Items.Count-1 do begin
        if (OpenDialog1.Files[i]=ListBox1.Items[j]) then begin
          neni :=false;
          Application.MessageBox('To uz tam mas!', PAnsiCHar(ExtractFileName(ListBox1.Items[j])));
        end;
      end;
      if (neni = true) then
          ListBox1.Items.Add(OpenDialog1.Files[i]);
    end;
  end;
  nacteni:=false;
  end;
end;

procedure TMainForm.Odebrat1Click(Sender: TObject);
var i:integer;
begin
  for i:=listBox1.Items.Count-1 downto 0 do
    if listBox1.Selected[i] then listBox1.Items.Delete(i);
end; }

{procedure TMainForm.Zobrazit1Click(Sender: TObject);
var i:integer;
begin
if nacteni=true then begin
  for i:=0 to listBox1.Items.Count-1 do begin
    if listBox1.Selected[i] then
      with TImageForm.Create(Self) do begin
        Image1.Picture.Bitmap:= obrazek[i];
        Caption:= ExtractFileName(ListBox1.Items[i]);
        Image1.Repaint;
      end;
  end;
end
else Application.MessageBox('Nejdriv nacti obrazky!','Nenacteno!!!');
end;

procedure TMainForm.Natenobrzk1Click(Sender: TObject);
var Adr,i,j,k:integer;
    SL: PByteArray;
begin
  if ListBox1.Items.Count = 0 then nacteni:=false
  else begin
    for i:=0 to listBox1.Items.Count-1 do begin
      TestTIFFile(ListBox1.Items[i],TIFFinfo);
      //W:=GetTIFFWidth(TIFFinfo: TTIFFinfo; Index: Word): Integer;    //sirka snimku
      //H:=GetTIFFHeight(TIFFinfo: TTIFFinfo; Index: Word): Integer;   //vyska snimku
      LoadFromFileTIF(ListBox1.Items[i],1,Image16Array,TIFFinfo);
      obrazek[i] := TBitMap.Create;
      obrazek[i].PixelFormat:=pf24bit;
      obrazek[i].Width:=TIFFinfo[1].Width.Value;
      obrazek[i].Height:=TIFFinfo[1].Height.Value;
      if TypeTIFF=imRGB16 then begin
        for j:=0 to TIFFinfo[1].Height.Value-1 do begin
          SL:=obrazek[i].ScanLine[j];
          Adr:=j*TIFFinfo[1].Width.Value*3;
          for k:=0 to TIFFinfo[1].Width.Value*3-1 do begin
            SL^[k]:=Round(Image16Array^[Adr+k]/256);
          end;
        end;
      end;
    end;
    nacteni:=true;
  end;
end;

procedure TMainForm.Zvislostvzdlenostinabarv1Click(Sender: TObject);
var i,j,k,poc,c:integer;
    SL: PByteArray;
    Vadne,PomocR,PomocG,PomocB:TMatice;
    //Vadne:array[1..500,1..500] of boolean;
    R,G,B,max,min:Word;
    ce:double;
    dc,moc:Double;
    Saturace:single;
begin
  if nacteni = true then begin
    MinX:= miniX;
    MaxX:= MaxiX;
    MinY:= miniY;
    MaxY:= maxiY;
    Vadne:=TMatice.Create(MaxY-MinY+1,MaxX-MinX+1);
    t:=TMatice.Create(ListBox1.Items.Count,1);
    y:=TMatice.Create(ListBox1.Items.Count,1);
    poc:=0;
    for j:=MinY to MaxY do begin
      SL:=obrazek[0].ScanLine[j];
      for k:=MinX to MaxX do begin
          B:=SL^[k*3];
          G:=SL^[k*3+1];
          R:=SL^[k*3+2];
          Saturace:=1-((Minimum(R,G,B))/(Maximum(R,G,B)));
          if (Saturace>=0.8) then begin
            Vadne.elem[j-MinY+1,k-MinX+1]:=0; //Pixel je v poradku
            Inc(poc);
          end
          else begin Vadne.elem[j-MinY+1,k-MinX+1]:=1; end; //Pixel je vadny
        end;
    end;
    PomocR:=TMatice.Create(1,poc);
    PomocG:=TMatice.Create(1,poc);
    PomocB:=TMatice.Create(1,poc);
    for i:=0 to ListBox1.Items.Count-1 do begin
      t.elem[i+1,1]:=i+1;
      c:=0;
      for j:=MinY to MaxY do begin
        SL:=obrazek[i].ScanLine[j];
        for k:=MinX to MaxX do begin
          if Vadne.elem[j-MinY+1,k-MinX+1]=0 then begin
            Inc(c);
            PomocB.elem[1,c]:=SL^[k*3];
            PomocG.elem[1,c]:=SL^[k*3+1];
            PomocR.elem[1,c]:=SL^[k*3+2];
          end;
        end;
      end;
      R:=Round(PomocR.Prumer);
      G:=Round(PomocG.Prumer);
      B:=Round(PomocB.Prumer);
      max:=Maximum(R,G,B);
      min:=Minimum(R,G,B);
      if R=max then y.elem[i+1,1]:=(G-B)/(max-min);
      if G=max then y.elem[i+1,1]:=2+(B-R)/(max-min);
      if B=max then y.elem[i+1,1]:=4+(R-G)/(max-min);
      y.elem[i+1,1]:=60*y.elem[i+1,1];
      if (y.elem[i+1,1]<0) then y.elem[i+1,1]:=y.elem[i+1,1]+360;
    end;
    TGraphForm.Create(Self);
      y.s_uloz('C:\Users\Barbora\FSI\Magisterské studium\Diplomová práce\2016_03_22\y.txt');
    //Image1.Canvas.TextOut(12,Image1.Height-20-Round(((Image1.Height-20)/150)*3.6935),'3.6935');
  end;
end;}

procedure TMainForm.Otevt1Click(Sender: TObject);
var i: Integer;
begin
  OpenDialog1.Options := OpenDialog1.Options + [ofAllowMultiSelect];
  if OpenDialog1.Execute then begin
    ListBox1.Clear;
    for i:= 0 to OpenDialog1.Files.Count -1 do begin
      SetLength(obrazek, i + 1);
      SetLength(TIFFinfo, i + 1);
      ListBox1.Items.Add(extractfilename(OpenDialog1.Files[i]));
      TestTIFFile(OpenDialog1.Files[i],TIFFinfo[i]);
      LoadFromFileTIF(OpenDialog1.Files[i],1,obrazek[i],TIFFinfo[i]);
    end;
    bAnalyza := True;
  end;
end;

procedure TMainForm.Otevt2Click(Sender: TObject);
begin
  OpenDialog1.Options := OpenDialog1.Options - [ofAllowMultiSelect];
  if OpenDialog1.Execute then begin
    //SetLength(obrazek, 0);
    Shape1.Visible := False;
    ListBox1.Clear;
    SetLength(TIFFinfo, Length(TIFFinfo) + 1);
    ListBox1.Items.Add(extractfilename(OpenDialog1.FileName));
    TestTIFFile(OpenDialog1.FileName,TIFFinfo[Length(TIFFinfo)-1]);
    LoadFromFileTIF(OpenDialog1.FileName,1,snimek,TIFFinfo[Length(TIFFinfo) - 1]);
    bAnalyza := False;
    bZobrVysProf := False;
    Vytvoitvkovprofil1.Enabled := True;
    Vzorovy := ZobrazitTif(snimek, TIFFinfo[Length(TIFFinfo) - 1]);
    Image1.Picture.Bitmap := Vzorovy;
  end;
end;

// Procedura Otevrt1Click

procedure TMainForm.ListBox1DblClick(Sender: TObject);
begin
  if bAnalyza then begin
    CisloVzor:= ListBox1.ItemIndex;
    Vzorovy := ZobrazitTif(obrazek[CisloVzor], TIFFinfo[CisloVzor]);
  end else begin
    Vzorovy := ZobrazitTif(snimek, TIFFinfo[Length(TIFFinfo)-1]);
  end;
  Image1.Picture.Bitmap := Vzorovy;
  Image1.Visible := True;
  Exporthodnot1.Enabled := True;
  Graf1.Enabled := True;
  bZobrVysProf := False;
end;

procedure TMainForm.N3Dmodel1Click(Sender: TObject);
var
  model3D: TModelForm;
begin
  model3D := TModelForm.Create(Self);
  try
    model3D.MaticeVysek := VyskaVeSnimku;
    model3D.ShowModal;
  finally
    FreeAndNil(model3D);
  end;

end;

// Procedura ListBox1DblClick

procedure TMainForm.Exporthodnot1Click(Sender: TObject);
var RozptylHue, MaticeHue: TMatice;
    i, j, k, c, Adr, Adr2: Integer;
    SLVzor: PByteArray;
    R, G, B: Word;
    Graf: TGraphForm;
    StredX, StredY: Double;
    mr: TModalResult;
    Tiffinf: TTIFFinfo;
    Image16Array: PImage16ArrayRGB;
begin
  PrumerHue := TMatice.Create(ListBox1.Count,1);
  if (Shape1.Shape = stSquare) then begin
    for i := 0 to PrumerHue.m -1 do begin
      c := 0;
      Image16Array := obrazek[i];
      Tiffinf := TIFFinfo[i];
      for j := Shape1.Top+ScrollBox1.VertScrollBar.Position to Shape1.Top+Shape1.Height+ScrollBox1.VertScrollBar.Position do begin
        SLVzor := Image1.Picture.Bitmap.ScanLine[j];
        Adr := j * Tiffinf[1].Width.Value*3;
        for k := Shape1.Left+ScrollBox1.HorzScrollBar.Position to Shape1.Left+Shape1.Width+ScrollBox1.HorzScrollBar.Position do begin
          Adr2 := 3 * k;
          B:=SLVzor^[Adr2];
          G:=SLVzor^[Adr2+1];
          R:=SLVzor^[Adr2+2];
          if ((B = 255) and (G = 255) and (R = 255)) then begin
            B:=Image16Array^[Adr + Adr2];
            G:=Image16Array^[Adr + Adr2 + 1];
            R:=Image16Array^[Adr + Adr2 + 2];
            PrumerHue.elem[i,0] := PrumerHue.elem[i,0] + Hue(R,G,B);
            Inc(c);
          end;//if je na obrazku bila
        end; //k - sloupec
      end;//j - řádek
      try
        PrumerHue.elem[i,0] := PrumerHue.elem[i,0]/c;
      except
        ShowMessage('Špatně vybraná oblast');
        break;
      end;
    end;//i - prochazeni obrazku
  end// if square okolí

  else if (Shape1.Shape = stCircle ) then begin
    StredX := Shape1.Left+(Shape1.Width/2)+ScrollBox1.HorzScrollBar.Position;
    StredY := Shape1.Top+(Shape1.Height/2)+ScrollBox1.VertScrollBar.Position;
    for i := 0 to PrumerHue.m -1 do begin
      c := 0;
      Image16Array := obrazek[i];
      Tiffinf := TIFFinfo[i];
      for j := Shape1.Top+ScrollBox1.VertScrollBar.Position to Shape1.Top+Shape1.Height+ScrollBox1.VertScrollBar.Position do begin
          SLVzor := Image1.Picture.Bitmap.ScanLine[j];
          Adr := j * Tiffinf[1].Width.Value*3;
          for k := Shape1.Left+ScrollBox1.HorzScrollBar.Position to Shape1.Left+Shape1.Width+ScrollBox1.HorzScrollBar.Position do begin
            if (sqrt(sqr(k-StredX)+sqr(j-StredY))<= Shape1.Width) then begin
              Adr2 := 3 * k;
              B:=SLVzor^[Adr2];
              G:=SLVzor^[Adr2+1];
              R:=SLVzor^[Adr2+2];
              if ((B = 255) and (G = 255) and (R = 255)) then begin
                B:=Image16Array^[Adr + Adr2];
                G:=Image16Array^[Adr + Adr2 + 1];
                R:=Image16Array^[Adr + Adr2 + 2];
                PrumerHue.elem[i,0] := PrumerHue.elem[i,0] + Hue(R,G,B);
                Inc(c);
              end;//if je na obrazku bila
            end; // if vzdalenost
          end; //k - sloupec
      end;//j - řádek
      try
        PrumerHue.elem[i,0] := PrumerHue.elem[i,0]/c;
      except
        ShowMessage('Špatně vybraná oblast');
        break;
      end;
    end;//i - prochazeni obrazku
  end; // if circle okolí
  if SaveTextFileDialog1.Execute then begin
    PrumerHue.s_uloz(SaveTextFileDialog1.FileName);
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  bAnalyza := False;
  bKlik := False;
  bVysProf := False;
  bZobrVysProf := True;
  bGraf := False;
end;

procedure TMainForm.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  SL:PByteArray;
  R, G, B: Word; //barvy a jejich maximum a minimum
  Image16Array: PImage16ArrayRGB;
  Tiffinf: TTIFFinfo;
  Adr, k: Integer;
  Hu: Double;
begin
  if (Button = mbLeft) and  not bZobrVysProf then begin
    if bAnalyza then begin
      Image16Array := obrazek[CisloVzor];
      Tiffinf := TIFFinfo[CisloVzor];
    end else begin
      Image16Array := Snimek;
      Tiffinf := TIFFinfo[Length(TiffInfo)-1];
    end;
    bKlik := True;
    Adr:=Y*Tiffinf[1].Width.Value*3;
    k := 3 * X;
    B:=Image16Array^[Adr + k];
    G:=Image16Array^[Adr + k + 1];
    R:=Image16Array^[Adr + k + 2];
    Hu:=Hue(R,G,B);
    Label2.Caption:='Hue = '+ FloatToStr(Hu) + '°';
    ZobrazPixely(Image16Array, Tiffinf, Hu);
  end; // Stisk levého tlačítka myši

  if (Button = mBRight) and bAnalyza then begin
    Shape1.Left:=X-ScrollBox1.HorzScrollBar.Position;
    Shape1.Top:=Y-ScrollBox1.VertScrollBar.Position;
    Shape1.Visible:=True;
  end; //Stisk pravého tlačítka myši
end; // Procedura Image1MouseDown

procedure TMainForm.TrackBar1Change(Sender: TObject);
begin
  if bKlik and not bZobrVysProf then begin
    if bAnalyza then
      ZobrazPixely(obrazek[CisloVzor], TIFFinfo[CisloVzor], StrToFloat(Copy(Label2.Caption,7, Length(Label2.Caption) - 8)))
    else
      ZobrazPixely(snimek, TIFFinfo[Length(TIFFinfo)-1], StrToFloat(Copy(Label2.Caption,7, Length(Label2.Caption) - 8)))
  end;
end;

procedure TMainForm.Uloitaktulnsnmek1Click(Sender: TObject);
begin
  if SavePictureDialog1.Execute then begin
    Image1.Picture.SaveToFile(SavePictureDialog1.FileName);
  end;
end;

procedure TMainForm.Vytvoitvkovprofil1Click(Sender: TObject);
var
  info: TTIFFinfo;
  i, Adr, j, k: Integer;
  R, G, B: Word;
  Image16Array: PImage16ArrayRGB;
  SL: PByteArray;
  dHue, Max, Min, dJas, Posun, Meritko: Double;
  mr: Integer;
  MatHue: TMatice;
  Barva: Byte;
begin
  if bVysProf then begin
    mr := MessageDlg('Vyskovy profil byl jiz vytvoren!' + #13#10 + 'ANO - znovu vytvori vyskovy profil' +#13#10 + 'NE - zobrazi vyskovy profil',
                        mtWarning, mbYesNoCancel, 0);
    case mr of
      mrYes: bVysProf := False;
      mrCancel: EXIT;
    end;
  end;
  if not bVysProf then begin
    info := TIFFinfo[Length(TIFFinfo)-1];
    SetLength(ZnamVysku, info[1].Height.Value, info[1].Width.Value);
    VyskaVeSnimku := TMatice.Create(info[1].Height.Value, info[1].Width.Value);
    //SetLength(VyskaVeSnimku, info[1].Height.Value, info[1].Width.Value);
    MatHue := TMatice.Create(info[1].Height.Value, info[1].Width.Value);

    Image16Array := snimek;
    for i:=0 to info[1].Height.Value-1 do begin
      Adr:=i*info[1].Width.Value*3;
      for j:= 0 to info[1].Width.Value-1 do begin
        k := 3 * j;
        B:=Image16Array^[Adr + k];
        G:=Image16Array^[Adr + k + 1];
        R:=Image16Array^[Adr + k + 2];
        Max:=MaximumRGB(R,G,B);
        Min:=MinimumRGB(R,G,B);
        dJas := Jas(R, G, B);
        if (Max<>Min) and (dJas>=TrackBar2.Position/100) then begin
          dHue:=Hue(R, G, B);
          ZnamVysku[i, j] := True;
          MatHue.elem[i ,j] := dHue;
        end else
          ZnamVysku[i, j] := False;
      end; //for j
    end; //for i

    Max := MatHue.Maximum;
    Min := MatHue.MinimumBezNul;
    Posun := InvTransfX(InvFunkce(apar, bpar, cpar, dpar, epar, TransfY(Min)));
    Meritko := InvTransfX(InvFunkce(apar, bpar, cpar, dpar, epar, TransfY(Max))) - Posun + 1;
    bmpVysProfil := TBitmap.Create;
    bmpVysProfil.PixelFormat := pf24bit;
    bmpVysProfil.SetSize(info[1].Width.Value, info[1].Height.Value);
    for i:=0 to MatHue.m - 1 do begin
      SL:=bmpVysProfil.ScanLine[i];
      for j:= 0 to MatHue.n - 1 do begin
        k := 3 * j;
        if ZnamVysku[i, j] then begin
          VyskaVeSnimku.elem[i, j] := InvTransfX(InvFunkce(apar, bpar, cpar, dpar, epar, TransfY(MatHue.elem[i, j])))
                                        - Posun + 1;
          Barva := Round((VyskaVeSnimku.elem[i, j]*255)/Meritko);
          SL^[k] := Barva;
          SL^[k+1] := Barva;
          SL^[k+2] := Barva;
        end else begin
          SL^[k] := 0;
          SL^[k+1] := 0;
          SL^[k+2] := 0;
        end;
      end; //for j
    end; //for i

    bVysProf := True;
  end;
  bZobrVysProf := True;
  Image1.Picture.Bitmap.Assign(bmpVysProfil);
  Image1.Repaint;
  Konvoluce1.Enabled := True;
  //N3Dmodel1.Enabled := True;
end;

function TMainForm.ZobrazitTif(Image16Array: PImage16ArrayRGB; Tiffinf: TTIFFinfo): TBitmap;
var
  SL: PByteArray;
  j, k, Adr: Integer;
begin
  Result := TBitmap.Create;
  Result.PixelFormat := pf24bit;
  Result.Width := TIFFinf[1].Width.Value;
  Result.Height := TIFFinf[1].Height.Value;
  for j:=0 to Tiffinf[1].Height.Value-1 do begin
    SL:=Result.ScanLine[j];
    Adr:=j*Tiffinf[1].Width.Value*3;
    for k:=0 to Tiffinf[1].Width.Value*3-1 do begin
      SL^[k]:=Round(Image16Array^[Adr+k]/256);
    end; // for k
  end; // for j
end;

procedure TMainForm.ZobrazPixely(Image16Array: PImage16ArrayRGB; Tiffinf: TTIFFinfo; H: Double);
var i, j, k, Adr:Integer;
    SL:PByteArray;
    R, G, B, Max, Min:Word;
    HVyp: Double;
begin
  Image1.Picture.Bitmap := Vzorovy;
  for i:=0 to Tiffinf[1].Height.Value-1 do begin
      Adr:=i*Tiffinf[1].Width.Value*3;
      SL:=Image1.Picture.Bitmap.ScanLine[i];
      for j:= 0 to Tiffinf[1].Width.Value-1 do begin
        k := 3 * j;
        B:=Image16Array^[Adr + k];
        G:=Image16Array^[Adr + k + 1];
        R:=Image16Array^[Adr + k + 2];
        Max:=MaximumRGB(R,G,B);
        Min:=MinimumRGB(R,G,B);
        if (Max<>Min) and (Jas(R, G, B)>=TrackBar2.Position/100) then begin
          HVyp:=Hue(R, G, B);
          if (HVyp>=H-TrackBar1.Position/30)
            and (HVyp<=H+TrackBar1.Position/30) then begin
            SL^[j*3]:=255;
            SL^[j*3+1]:=255;
            SL^[j*3+2]:=255;
          end;
        end;
      end; //for j
    end; //for i
  Image1.Repaint;
end;

function MaximumRGB(R, G , B: Word): Word;
var MaxCislo: Word;
begin
  MaxCislo := R;
  if (MaxCislo < G) then MaxCislo := G;
  if (MaxCislo < B) then MaxCislo := B;
  Result := MaxCislo;
end;

function MinimumRGB(R ,G, B: Word): Word;
var MinCislo: Word;
begin
  MinCislo := R;
  if (MinCislo > G) then MinCislo := G;
  if (MinCislo > B) then MinCislo := B;
  Result := MinCislo;
end;

function Hue(R, G, B: Word): Double;
var MaxCislo,MinCislo:Word;
    h: Double;
begin
  MaxCislo := MaximumRGB(R, G, B);
  MinCislo := MinimumRGB(R, G, B);
  if (MaxCislo = R) then begin
    if (G >= B) then h := (G - B)/(MaxCislo - MinCislo)
    else  h:=(G - B)/(MaxCislo - MinCislo) + 6;
  end;
  if (MaxCislo = G) then h := 2 + (B - R)/(MaxCislo - MinCislo);
  if (MaxCislo = B) then h := 4 + (R - G)/(MaxCislo - MinCislo);
  Result := 60 * h;
end;

function Jas(R, G, B: Word): Double;
begin
  Result := ((R + G + B)/3)/(255*255);
end;

function InvFunkce(a, b, c, d, e, y: Double): Double;
begin
  Result := ((Tangent((Power((y - d)/c, 1/e) - 0.5)*Pi)) - b)/a;
end;

//Nastavení tvaru a velikosti okolí
procedure TMainForm.RadioGroup1Click(Sender: TObject);
begin
  if (RadioGroup1.ItemIndex = 0) then Shape1.Shape := stSquare;
  if (RadioGroup1.ItemIndex = 1) then Shape1.Shape := stCircle;
end;

procedure TMainForm.RadioGroup2Click(Sender: TObject);
begin
  Shape1.Height := StrToInt(RadioGroup2.Items[RadioGroup2.ItemIndex]);
  Shape1.Width := StrToInt(RadioGroup2.Items[RadioGroup2.ItemIndex]);
end;

//Analýza obrazu
procedure TMainForm.Analzaobrazu1Click(Sender: TObject);
var
    i, j, k, c, Adr, Adr2: Integer;
    SLVzor: PByteArray;
    R, G, B: Word;
    Graf: TGraphForm;
    StredX, StredY, Pomoc: Double;
    mr: TModalResult;
    Tiffinf: TTIFFinfo;
    Image16Array: PImage16ArrayRGB;
begin
  if not bGraf then begin
    PrumerHue := TMatice.Create(Length(obrazek),1);
    if (Shape1.Shape = stSquare) then begin
      for i := 0 to PrumerHue.m -1 do begin
        c := 0;
        Image16Array := obrazek[i];
        Tiffinf := TIFFinfo[i];
        for j := Shape1.Top+ScrollBox1.VertScrollBar.Position to Shape1.Top+Shape1.Height+ScrollBox1.VertScrollBar.Position do begin
          SLVzor := Image1.Picture.Bitmap.ScanLine[j];
          Adr := j * Tiffinf[1].Width.Value*3;
          for k := Shape1.Left+ScrollBox1.HorzScrollBar.Position to Shape1.Left+Shape1.Width+ScrollBox1.HorzScrollBar.Position do begin
            Adr2 := 3 * k;
            B:=SLVzor^[Adr2];
            G:=SLVzor^[Adr2+1];
            R:=SLVzor^[Adr2+2];
            if ((B = 255) and (G = 255) and (R = 255)) then begin
              B:=Image16Array^[Adr + Adr2];
              G:=Image16Array^[Adr + Adr2 + 1];
              R:=Image16Array^[Adr + Adr2 + 2];
              Pomoc := PrumerHue.elem[i, 0] + Hue(R,G,B);
              PrumerHue.elem[i,0] := Pomoc;
              Inc(c);
            end;//if je na obrazku bila
          end; //k - sloupec
        end;//j - řádek
        try
          PrumerHue.elem[i,0] := TransfY(PrumerHue.elem[i,0]/c);
        except
          ShowMessage('Špatně vybraná oblast');
          break;
        end;
      end;//i - prochazeni obrazku
    end// if square okolí

    else if (Shape1.Shape = stCircle ) then begin
      StredX := Shape1.Left+(Shape1.Width/2)+ScrollBox1.HorzScrollBar.Position;
      StredY := Shape1.Top+(Shape1.Height/2)+ScrollBox1.VertScrollBar.Position;
      for i := 0 to PrumerHue.m -1 do begin
        c := 0;
        Image16Array := obrazek[i];
        Tiffinf := TIFFinfo[i];
        for j := Shape1.Top+ScrollBox1.VertScrollBar.Position to Shape1.Top+Shape1.Height+ScrollBox1.VertScrollBar.Position do begin
            SLVzor := Image1.Picture.Bitmap.ScanLine[j];
            Adr := j * Tiffinf[1].Width.Value*3;
            for k := Shape1.Left+ScrollBox1.HorzScrollBar.Position to Shape1.Left+Shape1.Width+ScrollBox1.HorzScrollBar.Position do begin
              if (sqrt(sqr(k-StredX)+sqr(j-StredY))<= Shape1.Width) then begin
                Adr2 := 3 * k;
                B:=SLVzor^[Adr2];
                G:=SLVzor^[Adr2+1];
                R:=SLVzor^[Adr2+2];
                if ((B = 255) and (G = 255) and (R = 255)) then begin
                  B:=Image16Array^[Adr + Adr2];
                  G:=Image16Array^[Adr + Adr2 + 1];
                  R:=Image16Array^[Adr + Adr2 + 2];
                  PrumerHue.elem[i,0] := PrumerHue.elem[i,0] + Hue(R,G,B);
                  Inc(c);
                end;//if je na obrazku bila
              end; // if vzdalenost
            end; //k - sloupec
        end;//j - řádek
        try
          PrumerHue.elem[i,0] := TransfY(PrumerHue.elem[i,0]/c);
        except
          ShowMessage('Špatně vybraná oblast');
          break;
        end;
      end;//i - prochazeni obrazku
    end; // if circle okolí
  end;

  Graf := TGraphForm.Create(PrumerHue);
  try
  mr := Graf.ShowModal;
  if mr = mrOk then begin
    apar := StrToFloat(Graf.Edit1.Text);
    bpar := StrToFloat(Graf.Edit2.Text);
    cpar := StrToFloat(Graf.Edit3.Text);
    dpar := StrToFloat(Graf.Edit4.Text);
    epar := StrToFloat(Graf.Edit5.Text);
    Soubor1.Enabled := True;
  end;
  finally
    FreeAndNil(Graf);
  end;
end;

// Uzavření formu pomocí Položky Konec v Menu Soubor
procedure TMainForm.Konec1Click(Sender: TObject);
begin
  MainForm.Close;
end;

procedure TMainForm.Konvoluce1Click(Sender: TObject);
begin
  N3Dmodel1.Enabled := True;
end;

end.
