unit WeightImageUnit;

interface

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

type
  TWeightImageForm = class(TForm)
    Image: TImage;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure Zoom(ImBr: TByteMatrix; X, Y: Integer);
    procedure Bounds(XY, ImHW: Integer; var XY0, XY1: Integer);
  public
    constructor Create(AOwner: TComponent); reintroduce;
    procedure ShowWeights(ImBr: TByteMatrix; X, Y: Integer);
  end;

var
  Send: Boolean;
  WeightImageForm: TWeightImageForm;

implementation

{$R *.dfm}
constructor TWeightImageForm.Create(AOwner: TComponent);
begin
  inherited;
  Image.Canvas.Pen.Color:=cWeightColor;
  Image.Canvas.Pen.Width:=cWeightWidth;
  Send:=True;
end;



procedure TWeightImageForm.ShowWeights(ImBr: TByteMatrix; X, Y: Integer);
var Center, Hig, i: Integer;
    Scale_DeMax: Double;
    De: TFloatArray;
begin
  Zoom(ImBr, X, Y);

  if (Y < Directions.DisInt) or (Y > High(ImBr)-Directions.DisInt) or
    (X < Directions.DisInt) or (X > High(ImBr[0])-Directions.DisInt) then begin
    Label1.Visible:=True;
    Label2.Visible:=True;
    Exit;
  end;
  Label1.Visible:=False;
  Label2.Visible:=False;

  SetLength(De, 0);
  De:=Directions.CountWeights(ImBr, X, Y);

  Center:=Round((cSize+0.5)*cFactor-0.5);
  Hig:=High(De);
  Scale_DeMax:=cScale/De[Hig];

  i:=Hig-1;
  Image.Canvas.MoveTo(Center,Center);
  Image.Canvas.LineTo(Center+Round(Scale_DeMax*De[0]*Directions.CosA[0]),Center-Round(Scale_DeMax*De[0]*Directions.SinA[0]));
  Image.Canvas.LineTo(Center+Round(Scale_DeMax*De[i]*Directions.CosA[i]),Center-Round(Scale_DeMax*De[i]*Directions.SinA[i]));
  for i:=1 to Hig-1 do begin
    Image.Canvas.MoveTo(Center,Center);
    Image.Canvas.LineTo(Center+Round(Scale_DeMax*De[i]*Directions.CosA[i]),Center-Round(Scale_DeMax*De[i]*Directions.SinA[i]));
    Image.Canvas.LineTo(Center+Round(Scale_DeMax*De[i-1]*Directions.CosA[i-1]),Center-Round(Scale_DeMax*De[i-1]*Directions.SinA[i-1]));
  end;

  De:=nil;
end;



procedure TWeightImageForm.Zoom(ImBr: TByteMatrix; X, Y: Integer);
var ImBrH, ImBrW, S2F, X0, X1, Y0, Y1, i, j, k, l, m, X0j, Y0i: Integer;
    SL: array of PByteArray;
begin
  ImBrH:=High(ImBr)+1;
  ImBrW:=High(ImBr[0])+1;
  Bounds(Y, ImBrH, Y0, Y1);
  Bounds(X, ImBrW, X0, X1);

  SetLength(SL, cFactor);

  S2F:=(2*cSize+1)*cFactor;
  ClientHeight:=S2F;
  ClientWidth:=S2F;
  Image.Height:=S2F;
  Image.Width:=S2F;
  Image.Picture.Bitmap.Height:=S2F;
  Image.Picture.Bitmap.Width:=S2F;
  Image.Picture.Bitmap.PixelFormat:=pf24bit;
  Label1.Left:=Round(S2F/2)-47;
  Label1.Top:=Round(S2F/2)-13;
  Label2.Left:=Round(S2F/2)-65;
  Label2.Top:=Round(S2F/2);

  for i:=0 to S2F-1 do begin
    SL[0]:=Image.Picture.Bitmap.ScanLine[i];
    for j:=0 to S2F-1 do
      for k:=0 to 2 do
        SL[0,3*j+k]:=50;
  end;

  for i:=Y0 to Y1 do begin
    Y0i:=i-Y+cSize;
    for k:=0 to cFactor-1 do
      SL[k]:=Image.Picture.Bitmap.ScanLine[cFactor*Y0i+k];
    for j:=X0 to X1 do begin
      X0j:=j-X+cSize;
      for k:=0 to cFactor-1 do
        for l:=0 to cFactor-1 do
          for m:=0 to 2 do
            SL[k,3*cFactor*X0j+3*l+m]:=128+Round(ImBr[i,j]/2);
    end;
  end;
  Image.Repaint;
  SL:=nil;
end;



procedure TWeightImageForm.Bounds(XY, ImHW: Integer; var XY0, XY1: Integer);
begin
  XY0:=XY-cSize;
  XY1:=XY+cSize;
  if XY0 < 0 then
    XY0:=0;
  if XY1 > ImHW-1 then
    XY1:=ImHW-1;
end;



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

end.
