unit ImageUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StrUtils, ComCtrls, StdCtrls, LibTiffDelphiHigherLevel,
  TypesUnit, HistoUnit, WeightImageUnit;

type

TImageForm = class(TForm)
    Image: TImage;
    Label1: TLabel;
    procedure Mouse(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure MouseClick(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    pImHeight: Integer;
    pImWidth: Integer;
    pImageBr: TByteMatrix;
    pBimodality: Integer;
    CloseAction: TCloseAction;
  protected
    HistoForm: THistoForm;
  public
    constructor Create(AOwner: TComponent; FileN: String); reintroduce; overload;
    constructor Create(AOwner: TComponent; ImBr: TByteMatrix); reintroduce; overload;
    property ImHeight: Integer read pImHeight;
    property ImWidth: Integer read pImWidth;
    property ImageBr: TByteMatrix read pImageBr;
    property Bimodality: Integer read pBimodality;    
    procedure MakeHistogram(Visibility: Boolean);
    function Thresholding(Threshold: Integer): TByteMatrix;
  end;

implementation

{$R *.dfm}

constructor TImageForm.Create(AOwner: TComponent; FileN: String);
var i, j: Integer;
    Ext: String;
    SL: PByteArray;
begin
  inherited Create(AOwner);
  Ext:=ExtractFileExt(FileN);
  case AnsiIndexText(Ext, ['.bmp', '.tif']) of
    0 : Image.Picture.Bitmap.LoadFromFile(FileN);
    1 : Image.Picture.Bitmap:=ReadTIFFFileIntoBitmap(FileN);
    else begin
      ShowMessage('Tento typ souboru není podporován.');
      Close;
      Exit;
    end;
  end;
  pImHeight:=Image.Picture.Bitmap.Height;
  pImWidth:=Image.Picture.Bitmap.Width;
  Image.Height:=ImHeight;
  Image.Width:=ImWidth;
  Image.Picture.Bitmap.PixelFormat:=pf24bit;
  SetLength(pImageBr, ImHeight, ImWidth);
  for i:=0 to ImHeight-1 do begin
    SL:=Image.Picture.Bitmap.ScanLine[i];
    for j:=0 to ImWidth-1 do
      pImageBr[i,j]:=Round((SL[3*j]+SL[3*j+1]+SL[3*j+2])/3);
  end;
  CloseAction:=caFree;
  pBimodality:=-1;
end;



constructor TImageForm.Create(AOwner: TComponent; ImBr: TByteMatrix);
var i, j, k: Integer;
    SL: PByteArray;
begin
  inherited Create(AOwner);
  pImageBr:=ImBr;
  pImHeight:=High(ImBr)+1;
  pImWidth:=High(ImBr[0])+1;
  Image.Height:=ImHeight;
  Image.Width:=ImWidth;
  Image.Picture.Bitmap.Height:=ImHeight;
  Image.Picture.Bitmap.Width:=ImWidth;
  Image.Picture.Bitmap.PixelFormat:=pf24bit;
  for i:=0 to ImHeight-1 do begin
    SL:=Image.Picture.Bitmap.ScanLine[i];
    for j:=0 to ImWidth-1 do
      for k:=0 to 2 do
        SL[3*j+k]:=ImageBr[i,j];
  end;
end;



procedure TImageForm.MakeHistogram(Visibility: Boolean);
var i, j: Integer;
    H: THistogram;
begin
  if HistoForm = nil then begin
    FillChar(H, SizeOf(THistogram), 0);
    for i:=0 to ImHeight-1 do
      for j:=0 to ImWidth-1 do
        Inc(H[ImageBr[i,j]]);
    HistoForm:=THistoForm.Create(Self, H, Caption);
  end;
  HistoForm.Visible:=Visibility;
  pBimodality:=HistoForm.Bimodality;
end;



function TImageForm.Thresholding(Threshold: Integer): TByteMatrix;
var i, j: Integer;
begin
  SetLength(Result, ImHeight, ImWidth);
  for i:=0 to ImHeight-1 do
    for j:=0 to ImWidth-1 do
      if ImageBr[i,j] <= Threshold then
        Result[i,j]:=cThresholdDown
      else
        Result[i,j]:=cThresholdUp;
end;



procedure TImageForm.Mouse(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Label1.Caption:='x: '+IntToStr(X)+'; y: '+IntToStr(Y);
  if (WeightImageForm <> nil) and Send then
    WeightImageForm.ShowWeights(ImageBr, X, Y);
end;



procedure TImageForm.MouseClick(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Send:=not Send;
end;



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


end.
