unit UInput;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, ComCtrls, Forms, Dialogs,
  ExtCtrls, StdCtrls, FileCtrl, USpline, Spin, Gauges, ExtDlgs, UJVuBasics,
  jpeg;

type
  TFormInput = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;    TabSheet2: TTabSheet;    TabSheet3: TTabSheet;
    TabSheet5: TTabSheet;    TabSheet6: TTabSheet;    TabSheet7: TTabSheet;
    sbWeiss: TScrollBar;
    sbRot: TScrollBar;
    sbGruen: TScrollBar;
    sbBlau: TScrollBar;
    sbKontrast: TScrollBar;
    sbZoom: TScrollBar;
    Shape1: TShape;    Shape2: TShape;    Shape3: TShape;    Shape4: TShape;
    Label1: TLabel;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;
    Label5: TLabel;    Label6: TLabel;    Label7: TLabel;    Label8: TLabel;
    Label9: TLabel;    Label10: TLabel;    Label11: TLabel;    Label12: TLabel;
    Label13: TLabel;    Label14: TLabel;    Label15: TLabel;    Label16: TLabel;
    Label17: TLabel;    Label18: TLabel;    Label19: TLabel;    Label20: TLabel;
    Label21: TLabel;    Label22: TLabel;    Label23: TLabel;    Label24: TLabel;
    Button1: TButton;    Button2: TButton;    Button3: TButton;
    sbComp: TScrollBar;
    Edit1: TEdit;    Edit2: TEdit;    Edit3: TEdit;
    Panel1: TPanel;    Panel2: TPanel;    Panel3: TPanel;
    pcft: TPanel;
    Shape5: TShape;    Shape6: TShape;    Shape7: TShape;
    cbRot: TCheckBox;    cbGruen: TCheckBox;    cbBlau: TCheckBox;
    cbhg: TCheckBox;    cbGlatt: TCheckBox;    cbz: TCheckBox;
    seRB: TSpinEdit;    seFB: TSpinEdit;    seFG: TSpinEdit;    seFR: TSpinEdit;
    seSB: TSpinEdit;    seVB: TSpinEdit;    seDreh: TSpinEdit;
    pbcft: TPaintBox;
    Image1: TImage;
    Image1b: TImage;
    OpenPictureDialog1: TOpenPictureDialog;
    TabSheet4: TTabSheet;
    Label25: TLabel;
    sbDreh: TScrollBar;
    Label26: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure sbWeissChange(Sender: TObject);
    procedure sbRotChange(Sender: TObject);
    procedure sbGruenChange(Sender: TObject);
    procedure sbBlauChange(Sender: TObject);
    procedure sbKontrastChange(Sender: TObject);
    procedure sbCompChange(Sender: TObject);
    procedure pbcftPaint(Sender: TObject);
    procedure Shape6MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Shape6MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
    procedure Shape6MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure sbZoomChange(Sender: TObject);
    procedure seFRChange(Sender: TObject);
    procedure cbhgClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure sbDrehChange(Sender: TObject);
  private
    { Private-Deklarationen }
    s6md: boolean;
    s6mx,s6my: integer;
    stuetz: TKoords2;
  public
    { Public-Deklarationen }
    MitVorschau: boolean;
    procedure MakeSpline;
    procedure DoRGB2(Bmp1:TBitmap; MitGauge:boolean);
    procedure ChangeRGB(Bmp1:TBitmap; MitGauge:boolean);
    procedure ChangeCS(Bmp1:TBitmap; MitGauge:boolean);
  end;

var
  FormInput: TFormInput;

implementation

{$R *.DFM}

procedure TFormInput.FormCreate(Sender: TObject);
var n:integer;
begin
  with PageControl1 do for n:=0 to PageCount-1 do Pages[n].TabVisible:=false;
  s6md:=false;
  SetLength(Stuetz[0],5);
  SetLength(Stuetz[1],5);
  MakeSpline;
  Image1.Picture.Bitmap.Width:=Image1.Width;
  Image1.Picture.Bitmap.Height:=Image1.Height;
  Image1.Picture.Bitmap.PixelFormat:=pf24bit;
  MitVorschau:=false;
end;

procedure TFormInput.FormShow(Sender: TObject);
begin
  Caption:=PageControl1.ActivePage.Caption;
  sbDreh.Tag:=1;
  sbDreh.Position:=0;
  sbDreh.Tag:=0;
  sbWeiss.Tag:=1;
  sbWeiss.Position:=0;
  sbWeiss.Tag:=0;
  sbKontrast.Tag:=1;
  sbKontrast.Position:=100;
  sbKontrast.Tag:=0;
  Panel2.Visible:=false;
  if MitVorschau then begin
    Height:=485;
    Panel2.Visible:=true;
  end else begin
    Height:=200;
    Panel2.Visible:=false;
  end;
  if FormInput.TabSheet7.Visible = true then begin
    Shape5.Left:=0; Shape5.Top:=100;
    Shape6.Left:=50; Shape6.Top:=50;
    Shape7.Left:=100; Shape7.Top:=0;
    MakeSpline;
  end;
end;

procedure TFormInput.ChangeRGB(Bmp1:TBitmap; MitGauge:boolean);
var x,y,n,r,g,b,dr,dg,db: Integer; P: PByteArray;
begin
  dr:=sbRot.Position;
  dg:=sbGruen.Position;
  db:=sbBlau.Position;
  for y := 0 to Bmp1.height -1 do begin
    P:=Bmp1.ScanLine[y];
    for x:=0 to Bmp1.width -1 do begin
      n:=x*3;
      r:=P[n+2]+dr;
      if r > 255 then r:=255; if r < 0 then r:=0;
      P[n+2]:=r;
      g:=P[n+1]+dg;
      if g > 255 then g:=255; if g < 0 then g:=0;
      P[n+1]:=g;
      b:=P[n]+db;
      if b > 255 then b:=255; if b < 0 then b:=0;
      P[n]:=b;
    end;
  end;
end;

procedure TFormInput.ChangeCS(Bmp1:TBitmap; MitGauge:boolean);
var x,y,n,i,f: Integer; dc,h0: extended; P: PByteArray;
begin
  dc:=sbKontrast.Position*0.01;
  for y := 0 to Bmp1.height -1 do begin
    P:=Bmp1.ScanLine[y];
    for x:=0 to Bmp1.width -1 do begin
      n:=x*3;
      h0:=0.0;
      for i:=0 to 2 do h0:=h0+P[n+i];
      h0:=h0/3;
      for i:=0 to 2 do begin
        f:=trunc(128+(P[n+i]-128)*dc);
        if f > 255 then f:=255; if f < 0 then f:=0;
        P[n+i]:=f;
      end;
    end;
  end;
end;

procedure TFormInput.sbWeissChange(Sender: TObject);
begin
  sbRot.Tag:=1;
  sbGruen.Tag:=1;
  sbBlau.Tag:=1;
  sbRot.Position:=sbRot.Position+sbWeiss.Position-Label1.Tag;
  sbGruen.Position:=sbGruen.Position+sbWeiss.Position-Label1.Tag;
  sbBlau.Position:=sbBlau.Position+sbWeiss.Position-Label1.Tag;
  Image1.Picture.Bitmap.Assign(Image1b.Picture.Bitmap);
  ChangeRGB(Image1.Picture.Bitmap, false);
  Image1.Repaint;
  sbRot.Tag:=0;
  sbGruen.Tag:=0;
  sbBlau.Tag:=0;
  Label1.Caption:=IntToStr(sbWeiss.Position);
  Label1.Tag:=sbWeiss.Position;
end;

procedure TFormInput.sbRotChange(Sender: TObject);
begin
  Label2.Caption:=IntToStr(sbRot.Position);
  Image1.Picture.Bitmap.Assign(Image1b.Picture.Bitmap);
  ChangeRGB(Image1.Picture.Bitmap, false);
  Image1.Repaint;
end;

procedure TFormInput.sbGruenChange(Sender: TObject);
begin
  Label3.Caption:=IntToStr(sbGruen.Position);
  Image1.Picture.Bitmap.Assign(Image1b.Picture.Bitmap);
  ChangeRGB(Image1.Picture.Bitmap, false);
  Image1.Repaint;
end;

procedure TFormInput.sbBlauChange(Sender: TObject);
begin
  Label4.Caption:=IntToStr(sbBlau.Position);
  Image1.Picture.Bitmap.Assign(Image1b.Picture.Bitmap);
  ChangeRGB(Image1.Picture.Bitmap, false);
  Image1.Repaint;
end;

procedure TFormInput.sbKontrastChange(Sender: TObject);
begin
  Label5.Caption:=IntToStr(sbKontrast.Position);
  Image1.Picture.Bitmap.Assign(Image1b.Picture.Bitmap);
  ChangeCS(Image1.Picture.Bitmap, false);
  Image1.Repaint;
end;

procedure TFormInput.sbCompChange(Sender: TObject);
begin
  Label12.Caption:='Bildqualität: '+inttostr(sbComp.Position)+'%';
end;

procedure TFormInput.pbcftPaint(Sender: TObject);
var f,x,y:integer;
begin
  pbcft.Canvas.Rectangle(0,0,101,101);
  x:=trunc(SplineXY.Calc(1).x/255*101);
  y:=101-trunc(SplineXY.Calc(1).y/255*101);
  pbcft.Canvas.MoveTo(x,y);
  for f:=10 to 30 do begin
    x:=trunc(SplineXY.Calc(f/10).x/255*101);
    y:=101-trunc(SplineXY.Calc(f/10).y/255*101);
    pbcft.Canvas.LineTo(x,y);
  end;
  Image1.Picture.Bitmap.Assign(Image1b.Picture.Bitmap);
  DoRGB2(Image1.Picture.Bitmap, false);
  Image1.Repaint;
end;

procedure TFormInput.DoRGB2(Bmp1:TBitmap; MitGauge:boolean);
var x,y: Integer; P: PByteArray;
    h:array[0..255] of integer;
begin
  for x:=0 to 255 do begin
    h[x]:=trunc(SplineXY.Calc(1.0+2.0*x/255.0).y);
    if h[x] > 255 then h[x]:=255;
    if h[x] < 0 then h[x]:=0;
  end;
  GlobalGauge.MaxValue:=Bmp1.height;
  if MitGauge then GlobalGauge.Visible:=true;
  for y:=0 to Bmp1.height-1 do begin
    P:=Bmp1.ScanLine[y];
    for x:=0 to Bmp1.width-1 do begin
      if cbBlau.Checked then P[x*3]:=h[P[x*3]];
      if cbGruen.Checked then P[x*3+1]:=h[P[x*3+1]];
      if cbRot.Checked then P[x*3+2]:=h[P[x*3+2]];
    end;
    GlobalGauge.Progress:=y;
  end;
  GlobalGauge.Visible:=false;
end;

procedure TFormInput.MakeSpline;
var i,b: integer;
begin
  b:=(Shape5.Width div 2)+1;
  Stuetz[0,0]:=2*Shape5.Left-Shape6.Left;
  Stuetz[1,0]:=2*Shape5.Top-Shape6.Top;
  Stuetz[0,1]:=Shape5.Left;
  Stuetz[1,1]:=Shape5.Top;
  Stuetz[0,2]:=Shape6.Left;
  Stuetz[1,2]:=Shape6.Top;
  Stuetz[0,3]:=Shape7.Left;
  Stuetz[1,3]:=Shape7.Top;
  Stuetz[0,4]:=2*Shape7.Left-Shape6.Left;
  Stuetz[1,4]:=2*Shape7.Top-Shape6.Top;
  for i:=0 to 4 do begin
    Stuetz[0,i]:=Stuetz[0,i]*255/100;
    Stuetz[1,i]:=(100-Stuetz[1,i])*255/100;
  end;
  SplineXY.Init(Stuetz,0);
end;


procedure TFormInput.Shape6MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  s6md:=false;
end;

procedure TFormInput.Shape6MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if s6md = false then exit;
  with (Sender as TShape) do begin
    Top:=Top-s6my+y;
    if Top < 0 then Top:=0;
    if Top > 100 then Top:=100;
    if Left > 0 then if Left < 100 then begin
      Left:=Left-s6mx+x;
      if Left < 1 then Left:=1;
      if Left > 99 then Left:=99;
    end;
    MakeSpline;
    Pbcft.Repaint;
  end;
end;

procedure TFormInput.Shape6MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  s6md:=true;
  s6mx:=x; s6my:=y;
end;

procedure TFormInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  MitVorschau:=false;
end;

procedure TFormInput.sbZoomChange(Sender: TObject);
begin
  Label18.Caption:=IntToStr(sbZoom.Position);
  Edit1.Text:=IntToStr(Round(StrToInt(Label15.Caption)*sbZoom.Position/100));
  Edit2.Text:=IntToStr(Round(StrToInt(Label16.Caption)*sbZoom.Position/100));
end;

procedure TFormInput.seFRChange(Sender: TObject);
begin
  Panel3.Color:=$02000000+seFB.Value*256*256+seFG.Value*256+seFR.Value;
end;

procedure TFormInput.cbhgClick(Sender: TObject);
var b,h: integer;
begin
  MitVorschau:=cbhg.Checked;
  if MitVorschau then begin
    Top:=Top-273;
    Height:=485;
    Panel2.Visible:=true;
  end else begin
    Height:=212;
    Top:=Top+273;
    Panel2.Visible:=false;
  end;
  if cbhg.Checked then if FileExists(Edit3.Text) then begin
    b:=Image1.Width;
    h:=Image1.Height;
    BildLaden(Edit3.Text);
    Image1.Picture.Bitmap.Canvas.StretchDraw(Rect(0,0,b,h),GlobalBmp);
  end;
end;

procedure TFormInput.Button1Click(Sender: TObject);
begin
  if not(OpenPictureDialog1.Execute) then exit;
  Edit3.Text:=OpenPictureDialog1.FileName;
  Top:=Top+273;
  cbhgClick(Sender);
end;

procedure TFormInput.sbDrehChange(Sender: TObject);
begin
  Label26.Caption:=IntToStr(sbDreh.Position);
  if Tag = 0 then BildDrehen(Image1.Picture.Bitmap,sbDreh.Position,true);
end;

end.

