unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Buttons, IOGraphics, Display, Global, Vcl.StdCtrls,
  Vcl.ComCtrls,math;

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    SpeedbuttonOpen: TSpeedButton;
    OpenDialog1: TOpenDialog;
    ScrollBox1: TScrollBox;
    Image1: TImage;
    Memo1: TMemo;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButtonGamma1: TSpeedButton;
    SpeedButtonGamma2: TSpeedButton;
    SpeedButtonGamma3: TSpeedButton;
    SpeedButtonBlack1: TSpeedButton;
    SpeedButtonBlack2: TSpeedButton;
    SpeedButtonBlack3: TSpeedButton;
    SpeedButtonWhite1: TSpeedButton;
    SpeedButtonWhite2: TSpeedButton;
    SpeedButtonWhite3: TSpeedButton;
    Panel_Y: TPanel;
    Panel_PixelIn: TPanel;
    Panel_PixelOut: TPanel;
    Panel_X: TPanel;
    SpeedButton_Crop_scale: TSpeedButton;
    EditFixX: TEdit;
    EditFixY: TEdit;
    EditPx: TEdit;
    EditPy: TEdit;
    ProgressBar1: TProgressBar;
    EditScale: TEdit;
    SpeedButtonShift: TSpeedButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure SpeedbuttonOpenClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButtonGamma1Click(Sender: TObject);
    procedure SpeedButtonBlack1Click(Sender: TObject);
    procedure SpeedButtonWhite1Click(Sender: TObject);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure SpeedButton_Crop_scaleClick(Sender: TObject);
    procedure SpeedButtonShiftClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var i,xx,yy:Integer;
    ImgWord:PWordArray;
begin
 ImgWord:=Img;
 xx:=x*Stretch;
 yy:=y*stretch;
 i:=xx+yy*Info.ImgWidth;
 panel_X.Caption:=InttoStr(xx);
 panel_Y.Caption:=InttoStr(yy);
 panel_PixelIn.Caption:=InttoStr(ImgWord^[i]);
 panel_PixelOut.Caption:=InttoStr(Image1.Picture.Bitmap.Canvas.Pixels[x,y] mod 256);
end;

procedure TForm2.SpeedButton1Click(Sender: TObject);
begin
Stretch:=(Sender as TSpeedButton).Tag;
DisplayBwImage(Image1,Info,Img,Stretch,true);
end;

procedure TForm2.SpeedButtonGamma1Click(Sender: TObject);
begin
gamma:= (Sender as TSpeedButton).Tag;
SetGammaCurve(Info,Black, White,gamma);
DisplayBwImage(Image1,Info,Img,Stretch,true);
end;


procedure TForm2.SpeedButtonBlack1Click(Sender: TObject);
begin
if sender=speedbuttonblack1 then black:=Info.Min_0Q1;
if sender=speedbuttonblack2 then black:=Info.Min_0Q01;
if sender=speedbuttonblack3 then black:=Info.Min_0Q001;
SetGammaCurve(Info,black, white,gamma);
DisplayBwImage(Image1,Info,Img,Stretch,true);
end;

procedure TForm2.SpeedbuttonOpenClick(Sender: TObject);
begin

if opendialog1.Execute
then begin
LoadImageFromFile(Opendialog1.FileName, Info,Img, 16, true, memo1, true);
SetGammaCurve(Info,Info.Min_0Q1,Info.Max_0Q01,-1);
DisplayBwImage(Image1,Info,Img,Stretch,true);
end;
Black:=Info.Min_0Q1;
White:=Info.Max_0Q01;
Gamma:=-1;
image1.Enabled:=true;
end;

procedure TForm2.SpeedButtonShiftClick(Sender: TObject);
var fixX, fixY: Integer;
    n,i,j,c,b,count, counter: Integer;
    px,py: extended;
    ImgWord: PWordArr;
    FPath: AnsiString;

begin
    ProgressBar1.Position:=0;
    InfoNEW:=Info;
    InfoNEW.ImgWidth:=256;
    InfoNEW.ImgHeight:=256;
    counter:= 0;
    fixX:=StrToInt(EditFixX.Text);
    fixY:=StrToInt (EditFixY.Text);
    py:=(StrToInt(EditPy.Text))/100;
    count:=round((100-StrToInt(EditPx.Text))*(100- (StrToInt(EditPy.Text)))/25);
    ImgWord:=Img;

    Getmem (ImgWordNEW,InfoNew.ImgWidth*InfoNEW.ImgHeight*2);
    While py < 1 do
      begin
        px:= (StrToInt(EditPx.Text))/100;
        While px < 1 do
          begin
            c:=0;
            b:=0;
            for j:=fixY to fixY+InfoNEW.ImgHeight-1 do
              begin
                for i:= fixX to fixX+InfoNEW.ImgWidth-1 do
                  begin
                    n:=j*Info.ImgWidth+i;
                    ImgWordNEW[c+b*256]:=round(ImgWord^[n]*(1-px)*(1-py)+
                    ImgWord^[n+1]*px*(1-py)+ImgWord^[n+Info.ImgWidth]*(1-px)*py+
                    ImgWord^[n+Info.ImgWidth+1]*px*py);
                    inc(c);
                  end;
                c:=0;
                inc(b);
              end;
            FPath:='C:\Users\Mívie\Documents\Škola\Třeťák\Bakalářka\Cropped\'+
            EditFixX.Text + '_' + EditFixY.Text + '_' + FloatToStr(Px*100) + '_' +
            FloatToStr(Py*100) + '.umf';
            //DisplayBwimage(Image1,InfoNew,Pointer(ImgWordNEW),stretch,true);  // vykreslí obraz - nedoporučeno při generovaání velkého množství obrazů
            SaveUMF(FPath,InfoNEW.ImgWidth,InfoNEW.ImgHeight,InfoNEW.BytesPerPixel,
            Pointer(ImgWordNEW));
            inc(counter,1);
            ProgressBar1.Position:=round(100*counter/count);
            px:=px+0.05;
          end;
        py:=py+0.05;
      end;

end;

procedure TForm2.SpeedButtonWhite1Click(Sender:TObject);
begin
if sender=speedbuttonwhite1 then white:=Info.Max_0Q1;
if sender=speedbuttonwhite2 then white:=Info.Max_0Q01;
if sender=speedbuttonwhite3 then white:=Info.Max_0Q001;
SetGammaCurve(Info,black, white,gamma);
DisplayBwImage(Image1,Info,Img,Stretch,true);
end;

procedure TForm2.SpeedButton_Crop_scaleClick(Sender: TObject);

var fixX, fixY: Integer;
    n,i,j,c,b,modul,k,l: Integer;
    p,Ca,Cb,Cc,Cd,Ce,Cf,pp: extended;
    ImgWord: PWordArr;
    FPath: AnsiString;

begin
    InfoNEW:=Info;
    InfoNEW.ImgWidth:=256;
    InfoNEW.ImgHeight:=256;
    fixX:=StrToInt(EditFixX.Text);
    fixY:=StrToInt (EditFixY.Text);
    ImgWord:=Img;
    p:=1/(1/(1-(StrToFloat(EditScale.Text))/100) - 1);
    modul:=0;
    pp:=p;

    Repeat
    inc(modul);
    pp:= frac(RoundTo((modul*p),-6));
    until  pp=0 ;

    Getmem (ImgWordNEW,InfoNew.ImgWidth*InfoNEW.ImgHeight*2);
    c:=0;
    b:=0;
    for j:=fixY to fixY+InfoNEW.ImgHeight-1 do
        begin
          l:=b mod modul;
          if (Trunc((l+1)*p)) > (Trunc(l*p)) then
            begin
            Cd:=Trunc((l+1)*p)-l*p;
            Ce:=1;
            Cf:=(l+1)*p-(Trunc((l+1)*p));
            end
          else
            begin
            Cd:=0;
            Ce:=(Trunc(l*p))+ 1 -l*p;
            Cf:=(l+1)*p - (Trunc(l*p));
            end;

          for i:= fixX to fixX+InfoNEW.ImgWidth-1 do
            begin
              k:=c mod modul;

              if (Trunc((k+1)*p))> (Trunc(k*p)) then
                begin
                Ca:=Trunc((k+1)*p)-k*p;
                Cb:=1;
                Cc:=(k+1)*p-(Trunc((k+1)*p));
                end
              else
                begin
                Ca:=0;
                Cb:=(Trunc(k*p))+ 1 -k*p;
                Cc:=(k+1)*p - (Trunc(k*p));
                end;

              n:=(j+(Trunc((b+1)*p)))*Info.ImgWidth+i+(Trunc((c+1)*p));
              ImgWordNEW[c+b*InfoNew.ImgWidth]:=round((ImgWord^[n-Info.ImgWidth-1]*Ca*Cd+
                        ImgWord^[n-Info.ImgWidth]*Cb*Cd+ImgWord^[n-Info.ImgWidth+1]*Cc*Cd+
                        ImgWord^[n-1]*Ca*Ce+ImgWord^[n]*Cb*Ce+ImgWord^[n+1]*Cc*Ce+
                        ImgWord^[n+Info.ImgWidth-1]*Ca*Cf+ImgWord^[n+Info.ImgWidth]*Cb*Cf+
                        ImgWord^[n+Info.ImgWidth+1]*Cc*Cf)/((1+p)*(1+p)));
              inc(c);
              end;
          c:=0;
          inc(b);
        end;
    FPath:='C:\Users\Mívie\Documents\Škola\Třeťák\Bakalářka\Cropped\'+'Scale' +
    EditScale.Text +'.umf';
    DisplayBwimage(Image1,InfoNew,Pointer(ImgWordNEW),stretch,true);  // vykreslí obraz
    SaveUMF(FPath,InfoNEW.ImgWidth,InfoNEW.ImgHeight,InfoNEW.BytesPerPixel,
    Pointer(ImgWordNEW));



end;

end.
